Package org.apache.flink.core.memory

Examples of org.apache.flink.core.memory.MemorySegment


   
    final int searchHashCode = hash(this.buildSideComparator.hash(record));
    final int posHashCode = searchHashCode % this.numBuckets;
   
    // get the bucket for the given hash code
    MemorySegment originalBucket = this.buckets[posHashCode >> this.bucketsPerSegmentBits];
    int originalBucketOffset = (posHashCode & this.bucketsPerSegmentMask) << NUM_INTRA_BUCKET_BITS;
    MemorySegment bucket = originalBucket;
    int bucketInSegmentOffset = originalBucketOffset;
   
    // get the basic characteristics of the bucket
    final int partitionNumber = bucket.get(bucketInSegmentOffset + HEADER_PARTITION_OFFSET);
    InMemoryPartition<T> partition = this.partitions.get(partitionNumber);
    final MemorySegment[] overflowSegments = partition.overflowSegments;
   
    this.buildSideComparator.setReference(record);
   
    int countInSegment = bucket.getInt(bucketInSegmentOffset + HEADER_COUNT_OFFSET);
    int numInSegment = 0;
    int posInSegment = bucketInSegmentOffset + BUCKET_HEADER_LENGTH;
   
    long currentForwardPointer = BUCKET_FORWARD_POINTER_NOT_SET;

    // loop over all segments that are involved in the bucket (original bucket plus overflow buckets)
    while (true) {
     
      while (numInSegment < countInSegment) {
       
        final int thisCode = bucket.getInt(posInSegment);
        posInSegment += HASH_CODE_LEN;
         
        // check if the hash code matches
        if (thisCode == searchHashCode) {
          // get the pointer to the pair
          final int pointerOffset = bucketInSegmentOffset + BUCKET_POINTER_START_OFFSET + (numInSegment * POINTER_LEN);
          final long pointer = bucket.getLong(pointerOffset);
          numInSegment++;
         
          // deserialize the key to check whether it is really equal, or whether we had only a hash collision
          try {
            tempHolder = partition.readRecordAt(pointer, tempHolder);
            if (this.buildSideComparator.equalToReference(tempHolder)) {
              long newPointer = partition.appendRecord(record);
              bucket.putLong(pointerOffset, newPointer);
              partition.setCompaction(false);
              if((newPointer >> this.pageSizeInBits) > this.compactionMemory.getBlockCount()) {
                this.compactionMemory.allocateSegments((int)(newPointer >> this.pageSizeInBits));
              }
              return;
            }
          } catch (EOFException e) {
            // system is out of memory so we attempt to reclaim memory with a copy compact run
            long newPointer;
            try {
              compactPartition(partition.getPartitionNumber());
              // retry append
              partition = this.partitions.get(partitionNumber); // compaction invalidates reference
              newPointer = partition.appendRecord(record);
            } catch (EOFException ex) {
              throw new RuntimeException("Memory ran out. Compaction failed. " +
                            getMemoryConsumptionString() +
                            " Message: " + ex.getMessage());
            } catch (IndexOutOfBoundsException ex) {
              throw new RuntimeException("Memory ran out. Compaction failed. " +
                            getMemoryConsumptionString() +
                            " Message: " + ex.getMessage());
            }
            bucket.putLong(pointerOffset, newPointer);
            return;
          } catch (IndexOutOfBoundsException e) {
            // system is out of memory so we attempt to reclaim memory with a copy compact run
            long newPointer;
            try {
              compactPartition(partition.getPartitionNumber());
              // retry append
              partition = this.partitions.get(partitionNumber); // compaction invalidates reference
              newPointer = partition.appendRecord(record);
            } catch (EOFException ex) {
              throw new RuntimeException("Memory ran out. Compaction failed. " +
                            getMemoryConsumptionString() +
                            " Message: " + ex.getMessage());
            } catch (IndexOutOfBoundsException ex) {
              throw new RuntimeException("Memory ran out. Compaction failed. " +
                            getMemoryConsumptionString() +
                            " Message: " + ex.getMessage());
            }
            bucket.putLong(pointerOffset, newPointer);
            return;
          } catch (IOException e) {
            throw new RuntimeException("Error deserializing record from the hashtable: " + e.getMessage(), e);
          }
        }
        else {
          numInSegment++;
        }
      }
     
      // this segment is done. check if there is another chained bucket
      long newForwardPointer = bucket.getLong(bucketInSegmentOffset + HEADER_FORWARD_OFFSET);
      if (newForwardPointer == BUCKET_FORWARD_POINTER_NOT_SET) {
        // nothing found. append and insert
        long pointer = partition.appendRecord(record);
        //insertBucketEntryFromStart(partition, originalBucket, originalBucketOffset, searchHashCode, pointer);
        insertBucketEntryFromSearch(partition, originalBucket, bucket, originalBucketOffset, bucketInSegmentOffset, countInSegment, currentForwardPointer, searchHashCode, pointer);
        if((pointer >> this.pageSizeInBits) > this.compactionMemory.getBlockCount()) {
          this.compactionMemory.allocateSegments((int)(pointer >> this.pageSizeInBits));
        }
        return;
      }
     
      final int overflowSegNum = (int) (newForwardPointer >>> 32);
      bucket = overflowSegments[overflowSegNum];
      bucketInSegmentOffset = (int) (newForwardPointer & 0xffffffff);
      countInSegment = bucket.getInt(bucketInSegmentOffset + HEADER_COUNT_OFFSET);
      posInSegment = bucketInSegmentOffset + BUCKET_HEADER_LENGTH;
      numInSegment = 0;
      currentForwardPointer = newForwardPointer;
    }
  }
View Full Code Here


      boolean isAsyncRequest = false;

      synchronized (this.buffers) {
        // Return excess buffers to global buffer pool
        while (this.numRequestedBuffers > this.numDesignatedBuffers) {
          final MemorySegment buffer = this.buffers.poll();
          if (buffer == null) {
            break;
          }

          this.globalBufferPool.returnBuffer(buffer);
          this.numRequestedBuffers--;
        }

        // Request buffers from global buffer pool
        while (this.buffers.isEmpty()) {
          if (this.numRequestedBuffers < this.numDesignatedBuffers) {
            final MemorySegment buffer = this.globalBufferPool.requestBuffer();

            if (buffer != null) {
              this.buffers.add(buffer);

              this.numRequestedBuffers++;
View Full Code Here

      if (originalForwardPointer != BUCKET_FORWARD_POINTER_NOT_SET) {
       
        // forward pointer set
        final int overflowSegNum = (int) (originalForwardPointer >>> 32);
        final int segOffset = (int) (originalForwardPointer & 0xffffffff);
        final MemorySegment seg = p.overflowSegments[overflowSegNum];
       
        final int obCount = seg.getInt(segOffset + HEADER_COUNT_OFFSET);
       
        // check if there is space in this overflow bucket
        if (obCount < NUM_ENTRIES_PER_BUCKET) {
          // space in this bucket and we are done
          seg.putInt(segOffset + BUCKET_HEADER_LENGTH + (obCount * HASH_CODE_LEN), hashCode)// hash code
          seg.putLong(segOffset + BUCKET_POINTER_START_OFFSET + (obCount * POINTER_LEN), pointer); // pointer
          seg.putInt(segOffset + HEADER_COUNT_OFFSET, obCount + 1); // update count
          return;
        } else {
          // no space here, we need a new bucket. this current overflow bucket will be the
          // target of the new overflow bucket
          forwardForNewBucket = originalForwardPointer;
        }
      } else {
        // no overflow bucket yet, so we need a first one
        forwardForNewBucket = BUCKET_FORWARD_POINTER_NOT_SET;
      }
     
      // we need a new overflow bucket
      MemorySegment overflowSeg;
      final int overflowBucketNum;
      final int overflowBucketOffset;
     
      // first, see if there is space for an overflow bucket remaining in the last overflow segment
      if (p.nextOverflowBucket == 0) {
        // no space left in last bucket, or no bucket yet, so create an overflow segment
        overflowSeg = getNextBuffer();
        overflowBucketOffset = 0;
        overflowBucketNum = p.numOverflowSegments;
       
        // add the new overflow segment
        if (p.overflowSegments.length <= p.numOverflowSegments) {
          MemorySegment[] newSegsArray = new MemorySegment[p.overflowSegments.length * 2];
          System.arraycopy(p.overflowSegments, 0, newSegsArray, 0, p.overflowSegments.length);
          p.overflowSegments = newSegsArray;
        }
        p.overflowSegments[p.numOverflowSegments] = overflowSeg;
        p.numOverflowSegments++;
        checkForResize = true;
      } else {
        // there is space in the last overflow bucket
        overflowBucketNum = p.numOverflowSegments - 1;
        overflowSeg = p.overflowSegments[overflowBucketNum];
        overflowBucketOffset = p.nextOverflowBucket << NUM_INTRA_BUCKET_BITS;
      }
     
      // next overflow bucket is one ahead. if the segment is full, the next will be at the beginning
      // of a new segment
      p.nextOverflowBucket = (p.nextOverflowBucket == this.bucketsPerSegmentMask ? 0 : p.nextOverflowBucket + 1);
     
      // insert the new overflow bucket in the chain of buckets
      // 1) set the old forward pointer
      // 2) let the bucket in the main table point to this one
      overflowSeg.putLong(overflowBucketOffset + HEADER_FORWARD_OFFSET, forwardForNewBucket);
      final long pointerToNewBucket = (((long) overflowBucketNum) << 32) | ((long) overflowBucketOffset);
      bucket.putLong(bucketInSegmentPos + HEADER_FORWARD_OFFSET, pointerToNewBucket);
     
      // finally, insert the values into the overflow buckets
      overflowSeg.putInt(overflowBucketOffset + BUCKET_HEADER_LENGTH, hashCode)// hash code
      overflowSeg.putLong(overflowBucketOffset + BUCKET_POINTER_START_OFFSET, pointer); // pointer
     
      // set the count to one
      overflowSeg.putInt(overflowBucketOffset + HEADER_COUNT_OFFSET, 1);
      if(checkForResize && !this.isResizing) {
        // check if we should resize buckets
        if(this.buckets.length <= getOverflowSegmentCount()) {
          resizeHashTable();
        }
View Full Code Here

      currentBucket.putInt(currentBucketOffset + BUCKET_HEADER_LENGTH + (countInCurrentBucket * HASH_CODE_LEN), hashCode)// hash code
      currentBucket.putLong(currentBucketOffset + BUCKET_POINTER_START_OFFSET + (countInCurrentBucket * POINTER_LEN), pointer); // pointer
      currentBucket.putInt(currentBucketOffset + HEADER_COUNT_OFFSET, countInCurrentBucket + 1); // update count
    } else {
      // we need a new overflow bucket
      MemorySegment overflowSeg;
      final int overflowBucketNum;
      final int overflowBucketOffset;
     
      // first, see if there is space for an overflow bucket remaining in the last overflow segment
      if (partition.nextOverflowBucket == 0) {
        // no space left in last bucket, or no bucket yet, so create an overflow segment
        overflowSeg = getNextBuffer();
        overflowBucketOffset = 0;
        overflowBucketNum = partition.numOverflowSegments;
       
        // add the new overflow segment
        if (partition.overflowSegments.length <= partition.numOverflowSegments) {
          MemorySegment[] newSegsArray = new MemorySegment[partition.overflowSegments.length * 2];
          System.arraycopy(partition.overflowSegments, 0, newSegsArray, 0, partition.overflowSegments.length);
          partition.overflowSegments = newSegsArray;
        }
        partition.overflowSegments[partition.numOverflowSegments] = overflowSeg;
        partition.numOverflowSegments++;
        checkForResize = true;
      } else {
        // there is space in the last overflow segment
        overflowBucketNum = partition.numOverflowSegments - 1;
        overflowSeg = partition.overflowSegments[overflowBucketNum];
        overflowBucketOffset = partition.nextOverflowBucket << NUM_INTRA_BUCKET_BITS;
      }
     
      // next overflow bucket is one ahead. if the segment is full, the next will be at the beginning
      // of a new segment
      partition.nextOverflowBucket = (partition.nextOverflowBucket == this.bucketsPerSegmentMask ? 0 : partition.nextOverflowBucket + 1);
     
      // insert the new overflow bucket in the chain of buckets
      // 1) set the old forward pointer
      // 2) let the bucket in the main table point to this one
      overflowSeg.putLong(overflowBucketOffset + HEADER_FORWARD_OFFSET, currentForwardPointer);
      final long pointerToNewBucket = (((long) overflowBucketNum) << 32) | ((long) overflowBucketOffset);
      originalBucket.putLong(originalBucketOffset + HEADER_FORWARD_OFFSET, pointerToNewBucket);
     
      // finally, insert the values into the overflow buckets
      overflowSeg.putInt(overflowBucketOffset + BUCKET_HEADER_LENGTH, hashCode)// hash code
      overflowSeg.putLong(overflowBucketOffset + BUCKET_POINTER_START_OFFSET, pointer); // pointer
     
      // set the count to one
      overflowSeg.putInt(overflowBucketOffset + HEADER_COUNT_OFFSET, 1);
      if(checkForResize && !this.isResizing) {
        // check if we should resize buckets
        if(this.buckets.length <= getOverflowSegmentCount()) {
          resizeHashTable();
        }
View Full Code Here

   */
  public MemorySegment getNextReturnedSegment() throws IOException
  {
    try {
      while (true) {
        final MemorySegment next = this.returnBuffers.poll(2000, TimeUnit.MILLISECONDS);
        if (next != null) {
          return next;
        } else {
          if (this.closed) {
            throw new IOException("The reader has been asynchronously closed.");
View Full Code Here

    this.segmentNum = 0;
  }
 
  @Override
  protected MemorySegment nextSegment(MemorySegment current, int positionInCurrent) throws EOFException {
    final MemorySegment next = this.memorySource.nextSegment();
    if (next != null) {
      this.fullSegments.add(next);
      this.segmentNum++;
      return next;
    } else {
View Full Code Here

  protected MemorySegment nextSegment(MemorySegment current, int positionInCurrent) throws IOException {
    // check if we are still in memory
    if (this.writer == null) {
      this.fullSegments.add(current);
     
      final MemorySegment nextSeg = this.memorySource.nextSegment();
      if (nextSeg != null) {
        return nextSeg;
      } else {
        // out of memory, need to spill: create a writer
        this.writer = this.ioManager.createBlockChannelWriter(this.ioManager.createChannel());
       
        // add all segments to the writer
        this.blockCount = this.fullSegments.size();
        this.numMemorySegmentsInWriter = this.blockCount;
        for (int i = 0; i < this.fullSegments.size(); i++) {
          this.writer.writeBlock(this.fullSegments.get(i));
        }
        this.fullSegments.clear();
        final MemorySegment seg = this.writer.getNextReturnedSegment();
        this.numMemorySegmentsInWriter--;
        return seg;
      }
    } else {
      // spilling
View Full Code Here

    }
    this.closed = true;
   
    // re-collect all memory segments
    ArrayList<MemorySegment> list = this.freeMem;
    final MemorySegment current = getCurrentSegment();
    if (current != null) {
      list.add(current);
    }
    clear();

    // close the writer and gather all segments
    final LinkedBlockingQueue<MemorySegment> queue = this.reader.getReturnQueue();
    this.reader.close();

    while (list.size() < this.numSegments) {
      final MemorySegment m = queue.poll();
      if (m == null) {
        // we get null if the queue is empty. that should not be the case if the reader was properly closed.
        throw new RuntimeException("Bug in ChannelReaderInputView: MemorySegments lost.");
      }
      list.add(m);
View Full Code Here

    if (current != null) {
      sendReadRequest(current);
    }
   
    // get the next segment
    final MemorySegment seg = this.reader.getNextReturnedSegment();
   
    // check the header
    if (seg.getShort(0) != ChannelWriterOutputView.HEADER_MAGIC_NUMBER) {
      throw new IOException("The current block does not belong to a ChannelWriterOutputView / " +
          "ChannelReaderInputView: Wrong magic number.");
    }
    if ( (seg.getShort(ChannelWriterOutputView.HEADER_FLAGS_OFFSET) & ChannelWriterOutputView.FLAG_LAST_BLOCK) != 0) {
      // last block
      this.numRequestsRemaining = 0;
      this.inLastBlock = true;
    }
   
View Full Code Here

    } else {
      this.numSegments = memory.size();
      // load the segments into the queue
      final LinkedBlockingQueue<MemorySegment> queue = writer.getReturnQueue();
      for (int i = memory.size() - 1; i >= 0; --i) {
        final MemorySegment seg = memory.get(i);
        if (seg.size() != segmentSize) {
          throw new IllegalArgumentException("The supplied memory segments are not of the specified size.");
        }
        queue.add(seg);
      }
    }
View Full Code Here

TOP

Related Classes of org.apache.flink.core.memory.MemorySegment

Copyright © 2018 www.massapicom. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.