Allocate a cluster for a new file

@return long the number of the newly allocated cluster
@throws IOException if there are no free clusters
167235
public long allocNew() throws IOException {

        int i;
        int entryIndex = -1;

        for (i = lastAllocatedCluster; i < lastClusterIndex; i++) {
            if (isFreeCluster(i)) {
                entryIndex = i;
                break;
            }
        }
        
        if (entryIndex < 0) {
            for (i = FIRST_CLUSTER; i < lastAllocatedCluster; i++) {
                if (isFreeCluster(i)) {
                    entryIndex = i;
                    break;
                }
            }
        }
        
        if (entryIndex < 0) {
            throw new IOException(
                    "FAT Full (" + (lastClusterIndex - FIRST_CLUSTER)
                    + ", " + i + ")"); //NOI18N
        }
        
        entries[entryIndex] = fatType.getEofMarker();
        lastAllocatedCluster = entryIndex % lastClusterIndex;
        if (lastAllocatedCluster < FIRST_CLUSTER)
            lastAllocatedCluster = FIRST_CLUSTER;
        
        return entryIndex;
    }
529771
private ComplexCondition allocate() {
    final ComplexCondition call = freeQueue.poll();
    return call != null ? call : new ComplexCondition(timeoutPeriod, timeoutUnits);
  }
188469
public long sizeInBytes() throws IOException {
        long memoryBlockUsage = 0;
        for (StreamBlock b : m_memoryDeque) {
            //Use only total size, but throw in the USO
            //to make book keeping consistent when flushed to disk
            //Also dont count persisted blocks.
            memoryBlockUsage += b.totalSize();
        }
        //Subtract USO from on disk size
        return memoryBlockUsage + m_reader.sizeInBytes() - (StreamBlock.HEADER_SIZE * m_reader.getNumObjects());
    }
198827
public CompletableFuture<NewFile> run() {
        LOG.log(Level.FINE, "starting optimized file creation for {0} files", files.size());
        CompletableFuture<NewFile> fileCreation = new CompletableFuture<>();
        final List<TSData> fjpFiles = this.files;  // We clear out files below, which makes createTmpFile see an empty map if we don't use a separate variable.
        TASK_POOL.execute(() -> createTmpFile(fileCreation, destDir, fjpFiles, getCompression()));
        synchronized (OUTSTANDING) {
            OUTSTANDING.add(fileCreation);
        }
        this.files = new LinkedList<>();  // Do not use clear! This instance is now shared with the createTmpFile task.
        return fileCreation;
    }
985974
public static void setAllocator(IoBufferAllocator newAllocator) {
    if (newAllocator == null) {
      throw new NullPointerException("allocator");
    }

    IoBufferAllocator oldAllocator = allocator;

    allocator = newAllocator;

    if (null != oldAllocator) {
      oldAllocator.dispose();
    }
  }
655496
ClusterConfiguration getLastSeenConfig() throws IOException {
    File file = getLatestFileWithPrefix(this.rootDir, "cluster_config");
    if (file == null) {
      return null;
    }
    try {
      Properties prop = FileUtils.readPropertiesFromFile(file);
      return ClusterConfiguration.fromProperties(prop);
    } catch (FileNotFoundException e) {
      LOG.debug("AckConfig file doesn't exist, probably it's the first time" +
          "bootup.");
    }
    return null;
  }
806899
@Override
	public int available() throws IOException {
		synchronized(file) {
			long len=file.getLength();
			return len>Integer.MAX_VALUE?Integer.MAX_VALUE:(int)len;
		}
	}
608447
long getDiskSpace(boolean free) throws IOException {
    long biggestSeenSoFar = 0;
    String[] localDirs = getLocalDirsFromConf(fConf);
    for (int i = 0; i < localDirs.length; i++) {
      DF df = null;
      if (localDirsDf.containsKey(localDirs[i])) {
        df = localDirsDf.get(localDirs[i]);
      } else {
        df = new DF(new File(localDirs[i]), fConf);
        localDirsDf.put(localDirs[i], df);
      }
      long onThisVol = free ? df.getAvailable() : df.getCapacity();
      if (onThisVol > biggestSeenSoFar) {
        biggestSeenSoFar = onThisVol;
      }
    }

    //Should ultimately hold back the space we expect running tasks to use but
    //that estimate isn't currently being passed down to the TaskTrackers
    return biggestSeenSoFar;
  }
1219427
@Override
    public AllocationPoint allocateMemory(DataBuffer buffer, AllocationShape requiredMemory, boolean initialize) {
        // by default we allocate on initial location
        AllocationPoint point = null;

        if (configuration.getMemoryModel() == Configuration.MemoryModel.IMMEDIATE) {
            point = allocateMemory(buffer, requiredMemory, memoryHandler.getInitialLocation(), initialize);
        } else if (configuration.getMemoryModel() == Configuration.MemoryModel.DELAYED) {
            // for DELAYED memory model we allocate only host memory, regardless of firstMemory configuration value
            point = allocateMemory(buffer, requiredMemory, AllocationStatus.HOST, initialize);
        }

        return point;
    }
696708
@Override
  public long getAllocatedMemory() {
    long sum = 0;
    for (MapInternals m : segments) {
      sum += m.getAllocatedMemory();
    }
    return sum;
  }
365137
public Token allocate(ManagedObject objectToStore)
                    throws ObjectManagerException
    {

        if (Tracing.isAnyTracingEnabled() && trace.isEntryEnabled())
            trace.entry(this,
                        cclass,
                        "allocate",
                        objectToStore);

        // We cannot store null, it won't serialize and we have no way to manage the transaction state.
        if (objectToStore == null) {
            if (Tracing.isAnyTracingEnabled() && trace.isEntryEnabled())
                trace.exit(this,
                           cclass,
                           "allocate",
                           objectToStore);
            throw new InvalidObjectToStoreException(this,
                                                    objectToStore);
        } // If asked to store null.

        // Is the store full?
        if (!allocationAllowed) {
            // request or wait for a checkpoint and see if that helps
            if (getStoreFullWaitForCheckPoint())
            {
                if (trace.isDebugEnabled())
                    trace.debug(this, cclass, "allocate", "Store is currently full, waiting for checkpoint");
                objectManagerState.waitForCheckpoint(true);
            }
            else
            {
                if (trace.isDebugEnabled())
                    trace.debug(this, cclass, "allocate", "Store is currently full, requesting checkpoint");
                objectManagerState.requestCheckpoint(true);
            }

            if (!allocationAllowed)
            {
                if (trace.isEntryEnabled())
                    trace.exit(this, cclass
                               , "allocate"
                                    );

                throw new ObjectStoreFullException(this
                                                   , objectToStore);
            }
        } // If (!allocationAllowed). 

        Token tokenToStore;
        long usableSequenceNumber;

        synchronized (sequenceNumberLock) {
            // Establish the sequence number we will use. 
            usableSequenceNumber = ++sequenceNumber;
        } // synchronized (sequenceNumberLock).

        // Create the container for the object.    
        tokenToStore = new Token(objectToStore, this, usableSequenceNumber);

        // Keep a note of the in memory copy of the object so that we give back the same one
        // whenever it is asked for.
        // If the application never adds the object, we would clean this up when its reference to the Object
        // is lost. 
        Token inMemoryToken = (Token) inMemoryTokens.putIfAbsent(new Long(usableSequenceNumber),
                                                                 tokenToStore);

        // Check that this genuinely a new storedObjectIdentifier.
        if (inMemoryToken != null) {
            if (Tracing.isAnyTracingEnabled() && trace.isDebugEnabled())
                trace.debug(this,
                            cclass,
                            "allocate",
                            new Object[] { "via StoreSequenceException",
                                          inMemoryToken,
                                          new Long(sequenceNumber) });
            StoreSequenceException storeSequenceException = new StoreSequenceException(this,
                                                                                       sequenceNumber,
                                                                                       inMemoryToken);
            ObjectManager.ffdc.processException(this,
                                                cclass,
                                                "add",
                                                storeSequenceException,
                                                "1:402:1.31");
            if (Tracing.isAnyTracingEnabled() && trace.isEntryEnabled())
                trace.exit(this,
                           cclass,
                           "allocate",
                           storeSequenceException);
            throw storeSequenceException;
        } // if (inMemoryToken != null).

        if (Tracing.isAnyTracingEnabled() && trace.isEntryEnabled())
            trace.exit(this, cclass
                       , "allocate"
                       , new Object[] { tokenToStore }
                            );
        return tokenToStore;
    }
955959
public Integer getAllocationSize()
   {
      if(childNode.getAttribute("allocation-size") != null && !childNode.getAttribute("allocation-size").equals("null"))
      {
         return Integer.valueOf(childNode.getAttribute("allocation-size"));
      }
      return null;
   }
500416
public long skip(long bytes) throws IOException {
        long pos = randomFile.getFilePointer();
        randomFile.seek(pos + bytes);
        return randomFile.getFilePointer() - pos;
    }
117828
public Cluster setCluster(Cluster newCluster) {
        // 开始切换
        Cluster old = this.cluster;
        this.cluster = newCluster;
        return old;
    }
1214759
protected long checkRegions(long offset, int length) throws IOException {
        if (offset + length > filledUpTo)
            throw new IOException("Can't access memory outside the file size (" + filledUpTo
                    + " bytes). You've requested portion " + offset + "-" + (offset + length) + " bytes. File: "
                    + osFile.getAbsolutePath() + "\n" + "actual Filesize: " + size + "\n" + "actual filledUpTo: "
                    + filledUpTo);
        return offset;
    }
1328361
private FileEntry allocate(int len) {
      synchronized (freeList) {
         // lookup a free entry of sufficient size
         SortedSet<FileEntry> candidates = freeList.tailSet(new FileEntry(0, len));
         for (Iterator<FileEntry> it = candidates.iterator(); it.hasNext(); ) {
            FileEntry free = it.next();
            // ignore entries that are still in use by concurrent readers
            if (free.isLocked())
               continue;

            // There's no race condition risk between locking the entry on
            // loading and checking whether it's locked (or store allocation),
            // because for the entry to be lockable, it needs to be in the
            // entries collection, in which case it's not in the free list.
            // The only way an entry can be found in the free list is if it's
            // been removed, and to remove it, lock on "entries" needs to be
            // acquired, which is also a pre-requisite for loading data.

            // found one, remove from freeList
            it.remove();
            return allocateExistingEntry(free, len);
         }

         // no appropriate free section available, append at end of file
         FileEntry fe = new FileEntry(filePos, len);
         filePos += len;
         if (trace) log.tracef("New entry allocated at %d:%d, %d free entries, file size is %d", fe.offset, fe.size, freeList.size(), filePos);
         return fe;
      }
   }
921344
public static long getSize(File file) {
        if (file.exists()) {
            if (!file.isFile()) {
                long size = 0;
                File[] files = file.listFiles();
                if (files != null && files.length > 0) {
                    for (File f : files) {
                        size += getSize(f);
                    }
                }
                return size;
            } else {
                return file.length();
            }
        }
        return 0;
    }