code
stringlengths 67
466k
| docstring
stringlengths 1
13.2k
|
---|---|
public int sendBuffer(SingleElementPushBackIterator<IN> input) throws IOException {
if (serializer == null) {
IN value = input.next();
serializer = getSerializer(value);
input.pushBack(value);
}
return sendBuffer(input, serializer);
} | Extracts records from an iterator and writes them to the memory-mapped file. This method assumes that all values
in the iterator are of the same type. This method does NOT take care of synchronization. The caller must
guarantee that the file may be written to before calling this method.
@param input iterator containing records
@return size of the written buffer
@throws IOException |
public static YarnHighAvailabilityServices forSingleJobAppMaster(
Configuration flinkConfig,
org.apache.hadoop.conf.Configuration hadoopConfig) throws IOException {
checkNotNull(flinkConfig, "flinkConfig");
checkNotNull(hadoopConfig, "hadoopConfig");
final HighAvailabilityMode mode = HighAvailabilityMode.fromConfig(flinkConfig);
switch (mode) {
case NONE:
return new YarnIntraNonHaMasterServices(flinkConfig, hadoopConfig);
case ZOOKEEPER:
throw new UnsupportedOperationException("to be implemented");
default:
throw new IllegalConfigurationException("Unrecognized high availability mode: " + mode);
}
} | Creates the high-availability services for a single-job Flink YARN application, to be
used in the Application Master that runs both ResourceManager and JobManager.
@param flinkConfig The Flink configuration.
@param hadoopConfig The Hadoop configuration for the YARN cluster.
@return The created high-availability services.
@throws IOException Thrown, if the high-availability services could not be initialized. |
public static YarnHighAvailabilityServices forYarnTaskManager(
Configuration flinkConfig,
org.apache.hadoop.conf.Configuration hadoopConfig) throws IOException {
checkNotNull(flinkConfig, "flinkConfig");
checkNotNull(hadoopConfig, "hadoopConfig");
final HighAvailabilityMode mode = HighAvailabilityMode.fromConfig(flinkConfig);
switch (mode) {
case NONE:
return new YarnPreConfiguredMasterNonHaServices(
flinkConfig,
hadoopConfig,
HighAvailabilityServicesUtils.AddressResolution.TRY_ADDRESS_RESOLUTION);
case ZOOKEEPER:
throw new UnsupportedOperationException("to be implemented");
default:
throw new IllegalConfigurationException("Unrecognized high availability mode: " + mode);
}
} | Creates the high-availability services for the TaskManagers participating in
a Flink YARN application.
@param flinkConfig The Flink configuration.
@param hadoopConfig The Hadoop configuration for the YARN cluster.
@return The created high-availability services.
@throws IOException Thrown, if the high-availability services could not be initialized. |
@Override
public List<MemorySegment> close() throws IOException {
if (this.closed) {
throw new IllegalStateException("Already closed.");
}
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);
}
return list;
} | Closes this InputView, closing the underlying reader and returning all memory segments.
@return A list containing all memory segments originally supplied to this view.
@throws IOException Thrown, if the underlying reader could not be properly closed. |
@Override
protected MemorySegment nextSegment(MemorySegment current) throws IOException {
// check if we are at our end
if (this.inLastBlock) {
throw new EOFException();
}
// send a request first. if we have only a single segment, this same segment will be the one obtained in
// the next lines
if (current != null) {
sendReadRequest(current);
}
// get the next segment
final MemorySegment seg = this.reader.getNextReturnedBlock();
// 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;
}
return seg;
} | Gets the next segment from the asynchronous block reader. If more requests are to be issued, the method
first sends a new request with the current memory segment. If no more requests are pending, the method
adds the segment to the readers return queue, which thereby effectively collects all memory segments.
Secondly, the method fetches the next non-consumed segment
returned by the reader. If no further segments are available, this method thrown an {@link EOFException}.
@param current The memory segment used for the next request.
@return The memory segment to read from next.
@throws EOFException Thrown, if no further segments are available.
@throws IOException Thrown, if an I/O error occurred while reading
@see AbstractPagedInputView#nextSegment(org.apache.flink.core.memory.MemorySegment) |
protected void sendReadRequest(MemorySegment seg) throws IOException {
if (this.numRequestsRemaining != 0) {
this.reader.readBlock(seg);
if (this.numRequestsRemaining != -1) {
this.numRequestsRemaining--;
}
} else {
// directly add it to the end of the return queue
this.freeMem.add(seg);
}
} | Sends a new read requests, if further requests remain. Otherwise, this method adds the segment
directly to the readers return queue.
@param seg The segment to use for the read request.
@throws IOException Thrown, if the reader is in error. |
public static <T> TypeInformation<T> of(Class<T> typeClass) {
try {
return TypeExtractor.createTypeInfo(typeClass);
}
catch (InvalidTypesException e) {
throw new FlinkRuntimeException(
"Cannot extract TypeInformation from Class alone, because generic parameters are missing. " +
"Please use TypeInformation.of(TypeHint) instead, or another equivalent method in the API that " +
"accepts a TypeHint instead of a Class. " +
"For example for a Tuple2<Long, String> pass a 'new TypeHint<Tuple2<Long, String>>(){}'.");
}
} | Creates a TypeInformation for the type described by the given class.
<p>This method only works for non-generic types. For generic types, use the
{@link #of(TypeHint)} method.
@param typeClass The class of the type.
@param <T> The generic type.
@return The TypeInformation object for the type described by the hint. |
public static int calculateFixLengthPartSize(InternalType type) {
if (type.equals(InternalTypes.BOOLEAN)) {
return 1;
} else if (type.equals(InternalTypes.BYTE)) {
return 1;
} else if (type.equals(InternalTypes.SHORT)) {
return 2;
} else if (type.equals(InternalTypes.INT)) {
return 4;
} else if (type.equals(InternalTypes.FLOAT)) {
return 4;
} else if (type.equals(InternalTypes.CHAR)) {
return 2;
} else if (type.equals(InternalTypes.DATE)) {
return 4;
} else if (type.equals(InternalTypes.TIME)) {
return 4;
} else {
// long, double is 8 bytes.
// It store the length and offset of variable-length part when type is string, map, etc.
return 8;
}
} | It store real value when type is primitive.
It store the length and offset of variable-length part when type is string, map, etc. |
public void connectSource(int inputNumber, IntermediateResult source, JobEdge edge, int consumerNumber) {
final DistributionPattern pattern = edge.getDistributionPattern();
final IntermediateResultPartition[] sourcePartitions = source.getPartitions();
ExecutionEdge[] edges;
switch (pattern) {
case POINTWISE:
edges = connectPointwise(sourcePartitions, inputNumber);
break;
case ALL_TO_ALL:
edges = connectAllToAll(sourcePartitions, inputNumber);
break;
default:
throw new RuntimeException("Unrecognized distribution pattern.");
}
inputEdges[inputNumber] = edges;
// add the consumers to the source
// for now (until the receiver initiated handshake is in place), we need to register the
// edges as the execution graph
for (ExecutionEdge ee : edges) {
ee.getSource().addConsumer(ee, consumerNumber);
}
} | -------------------------------------------------------------------------------------------- |
public Collection<CompletableFuture<TaskManagerLocation>> getPreferredLocations() {
Collection<CompletableFuture<TaskManagerLocation>> basedOnState = getPreferredLocationsBasedOnState();
return basedOnState != null ? basedOnState : getPreferredLocationsBasedOnInputs();
} | Gets the overall preferred execution location for this vertex's current execution.
The preference is determined as follows:
<ol>
<li>If the task execution has state to load (from a checkpoint), then the location preference
is the location of the previous execution (if there is a previous execution attempt).
<li>If the task execution has no state or no previous location, then the location preference
is based on the task's inputs.
</ol>
<p>These rules should result in the following behavior:
<ul>
<li>Stateless tasks are always scheduled based on co-location with inputs.
<li>Stateful tasks are on their initial attempt executed based on co-location with inputs.
<li>Repeated executions of stateful tasks try to co-locate the execution with its state.
</ul>
@see #getPreferredLocationsBasedOnState()
@see #getPreferredLocationsBasedOnInputs()
@return The preferred execution locations for the execution attempt. |
public Collection<CompletableFuture<TaskManagerLocation>> getPreferredLocationsBasedOnState() {
TaskManagerLocation priorLocation;
if (currentExecution.getTaskRestore() != null && (priorLocation = getLatestPriorLocation()) != null) {
return Collections.singleton(CompletableFuture.completedFuture(priorLocation));
}
else {
return null;
}
} | Gets the preferred location to execute the current task execution attempt, based on the state
that the execution attempt will resume.
@return A size-one collection with the location preference, or null, if there is no
location preference based on the state. |
public Collection<CompletableFuture<TaskManagerLocation>> getPreferredLocationsBasedOnInputs() {
// otherwise, base the preferred locations on the input connections
if (inputEdges == null) {
return Collections.emptySet();
}
else {
Set<CompletableFuture<TaskManagerLocation>> locations = new HashSet<>(getTotalNumberOfParallelSubtasks());
Set<CompletableFuture<TaskManagerLocation>> inputLocations = new HashSet<>(getTotalNumberOfParallelSubtasks());
// go over all inputs
for (int i = 0; i < inputEdges.length; i++) {
inputLocations.clear();
ExecutionEdge[] sources = inputEdges[i];
if (sources != null) {
// go over all input sources
for (int k = 0; k < sources.length; k++) {
// look-up assigned slot of input source
CompletableFuture<TaskManagerLocation> locationFuture = sources[k].getSource().getProducer().getCurrentTaskManagerLocationFuture();
// add input location
inputLocations.add(locationFuture);
// inputs which have too many distinct sources are not considered
if (inputLocations.size() > MAX_DISTINCT_LOCATIONS_TO_CONSIDER) {
inputLocations.clear();
break;
}
}
}
// keep the locations of the input with the least preferred locations
if (locations.isEmpty() || // nothing assigned yet
(!inputLocations.isEmpty() && inputLocations.size() < locations.size())) {
// current input has fewer preferred locations
locations.clear();
locations.addAll(inputLocations);
}
}
return locations.isEmpty() ? Collections.emptyList() : locations;
}
} | Gets the location preferences of the vertex's current task execution, as determined by the locations
of the predecessors from which it receives input data.
If there are more than MAX_DISTINCT_LOCATIONS_TO_CONSIDER different locations of source data, this
method returns {@code null} to indicate no location preference.
@return The preferred locations based in input streams, or an empty iterable,
if there is no input-based preference. |
public Execution resetForNewExecution(final long timestamp, final long originatingGlobalModVersion)
throws GlobalModVersionMismatch {
LOG.debug("Resetting execution vertex {} for new execution.", getTaskNameWithSubtaskIndex());
synchronized (priorExecutions) {
// check if another global modification has been triggered since the
// action that originally caused this reset/restart happened
final long actualModVersion = getExecutionGraph().getGlobalModVersion();
if (actualModVersion > originatingGlobalModVersion) {
// global change happened since, reject this action
throw new GlobalModVersionMismatch(originatingGlobalModVersion, actualModVersion);
}
final Execution oldExecution = currentExecution;
final ExecutionState oldState = oldExecution.getState();
if (oldState.isTerminal()) {
priorExecutions.add(oldExecution.archive());
final Execution newExecution = new Execution(
getExecutionGraph().getFutureExecutor(),
this,
oldExecution.getAttemptNumber() + 1,
originatingGlobalModVersion,
timestamp,
timeout);
currentExecution = newExecution;
synchronized (inputSplits) {
InputSplitAssigner assigner = jobVertex.getSplitAssigner();
if (assigner != null) {
assigner.returnInputSplit(inputSplits, getParallelSubtaskIndex());
inputSplits.clear();
}
}
CoLocationGroup grp = jobVertex.getCoLocationGroup();
if (grp != null) {
locationConstraint = grp.getLocationConstraint(subTaskIndex);
}
// register this execution at the execution graph, to receive call backs
getExecutionGraph().registerExecution(newExecution);
// if the execution was 'FINISHED' before, tell the ExecutionGraph that
// we take one step back on the road to reaching global FINISHED
if (oldState == FINISHED) {
getExecutionGraph().vertexUnFinished();
}
// reset the intermediate results
for (IntermediateResultPartition resultPartition : resultPartitions.values()) {
resultPartition.resetForNewExecution();
}
return newExecution;
}
else {
throw new IllegalStateException("Cannot reset a vertex that is in non-terminal state " + oldState);
}
}
} | Archives the current Execution and creates a new Execution for this vertex.
<p>This method atomically checks if the ExecutionGraph is still of an expected
global mod. version and replaces the execution if that is the case. If the ExecutionGraph
has increased its global mod. version in the meantime, this operation fails.
<p>This mechanism can be used to prevent conflicts between various concurrent recovery and
reconfiguration actions in a similar way as "optimistic concurrency control".
@param timestamp
The creation timestamp for the new Execution
@param originatingGlobalModVersion
@return Returns the new created Execution.
@throws GlobalModVersionMismatch Thrown, if the execution graph has a new global mod
version than the one passed to this message. |
public CompletableFuture<Void> scheduleForExecution(
SlotProvider slotProvider,
boolean queued,
LocationPreferenceConstraint locationPreferenceConstraint,
@Nonnull Set<AllocationID> allPreviousExecutionGraphAllocationIds) {
return this.currentExecution.scheduleForExecution(
slotProvider,
queued,
locationPreferenceConstraint,
allPreviousExecutionGraphAllocationIds);
} | Schedules the current execution of this ExecutionVertex.
@param slotProvider to allocate the slots from
@param queued if the allocation can be queued
@param locationPreferenceConstraint constraint for the location preferences
@param allPreviousExecutionGraphAllocationIds set with all previous allocation ids in the job graph.
Can be empty if the allocation ids are not required for scheduling.
@return Future which is completed once the execution is deployed. The future
can also completed exceptionally. |
void scheduleOrUpdateConsumers(ResultPartitionID partitionId) {
final Execution execution = currentExecution;
// Abort this request if there was a concurrent reset
if (!partitionId.getProducerId().equals(execution.getAttemptId())) {
return;
}
final IntermediateResultPartition partition = resultPartitions.get(partitionId.getPartitionId());
if (partition == null) {
throw new IllegalStateException("Unknown partition " + partitionId + ".");
}
partition.markDataProduced();
if (partition.getIntermediateResult().getResultType().isPipelined()) {
// Schedule or update receivers of this partition
execution.scheduleOrUpdateConsumers(partition.getConsumers());
}
else {
throw new IllegalArgumentException("ScheduleOrUpdateConsumers msg is only valid for" +
"pipelined partitions.");
}
} | Schedules or updates the consumer tasks of the result partition with the given ID. |
List<IntermediateResultPartition> finishAllBlockingPartitions() {
List<IntermediateResultPartition> finishedBlockingPartitions = null;
for (IntermediateResultPartition partition : resultPartitions.values()) {
if (partition.getResultType().isBlocking() && partition.markFinished()) {
if (finishedBlockingPartitions == null) {
finishedBlockingPartitions = new LinkedList<IntermediateResultPartition>();
}
finishedBlockingPartitions.add(partition);
}
}
if (finishedBlockingPartitions == null) {
return Collections.emptyList();
}
else {
return finishedBlockingPartitions;
}
} | Returns all blocking result partitions whose receivers can be scheduled/updated. |
boolean checkInputDependencyConstraints() {
if (getInputDependencyConstraint() == InputDependencyConstraint.ANY) {
// InputDependencyConstraint == ANY
return IntStream.range(0, inputEdges.length).anyMatch(this::isInputConsumable);
} else {
// InputDependencyConstraint == ALL
return IntStream.range(0, inputEdges.length).allMatch(this::isInputConsumable);
}
} | Check whether the InputDependencyConstraint is satisfied for this vertex.
@return whether the input constraint is satisfied |
boolean isInputConsumable(int inputNumber) {
return Arrays.stream(inputEdges[inputNumber]).map(ExecutionEdge::getSource).anyMatch(
IntermediateResultPartition::isConsumable);
} | Get whether an input of the vertex is consumable.
An input is consumable when when any partition in it is consumable.
Note that a BLOCKING result partition is only consumable when all partitions in the result are FINISHED.
@return whether the input is consumable |
void notifyStateTransition(Execution execution, ExecutionState newState, Throwable error) {
// only forward this notification if the execution is still the current execution
// otherwise we have an outdated execution
if (currentExecution == execution) {
getExecutionGraph().notifyExecutionChange(execution, newState, error);
}
} | Simply forward this notification. |
TaskDeploymentDescriptor createDeploymentDescriptor(
ExecutionAttemptID executionId,
LogicalSlot targetSlot,
@Nullable JobManagerTaskRestore taskRestore,
int attemptNumber) throws ExecutionGraphException {
// Produced intermediate results
List<ResultPartitionDeploymentDescriptor> producedPartitions = new ArrayList<>(resultPartitions.size());
// Consumed intermediate results
List<InputGateDeploymentDescriptor> consumedPartitions = new ArrayList<>(inputEdges.length);
boolean lazyScheduling = getExecutionGraph().getScheduleMode().allowLazyDeployment();
for (IntermediateResultPartition partition : resultPartitions.values()) {
List<List<ExecutionEdge>> consumers = partition.getConsumers();
if (consumers.isEmpty()) {
//TODO this case only exists for test, currently there has to be exactly one consumer in real jobs!
producedPartitions.add(ResultPartitionDeploymentDescriptor.from(
partition,
KeyGroupRangeAssignment.UPPER_BOUND_MAX_PARALLELISM,
lazyScheduling));
} else {
Preconditions.checkState(1 == consumers.size(),
"Only one consumer supported in the current implementation! Found: " + consumers.size());
List<ExecutionEdge> consumer = consumers.get(0);
ExecutionJobVertex vertex = consumer.get(0).getTarget().getJobVertex();
int maxParallelism = vertex.getMaxParallelism();
producedPartitions.add(ResultPartitionDeploymentDescriptor.from(partition, maxParallelism, lazyScheduling));
}
}
final InputChannelDeploymentDescriptor[] icddArray = new InputChannelDeploymentDescriptor[0];
for (ExecutionEdge[] edges : inputEdges) {
List<InputChannelDeploymentDescriptor> partitions = InputChannelDeploymentDescriptor.fromEdges(
Arrays.asList(edges),
lazyScheduling);
// If the produced partition has multiple consumers registered, we
// need to request the one matching our sub task index.
// TODO Refactor after removing the consumers from the intermediate result partitions
int numConsumerEdges = edges[0].getSource().getConsumers().get(0).size();
int queueToRequest = subTaskIndex % numConsumerEdges;
IntermediateResult consumedIntermediateResult = edges[0].getSource().getIntermediateResult();
final IntermediateDataSetID resultId = consumedIntermediateResult.getId();
final ResultPartitionType partitionType = consumedIntermediateResult.getResultType();
consumedPartitions.add(new InputGateDeploymentDescriptor(resultId, partitionType, queueToRequest, partitions.toArray(icddArray)));
}
final Either<SerializedValue<JobInformation>, PermanentBlobKey> jobInformationOrBlobKey = getExecutionGraph().getJobInformationOrBlobKey();
final TaskDeploymentDescriptor.MaybeOffloaded<JobInformation> serializedJobInformation;
if (jobInformationOrBlobKey.isLeft()) {
serializedJobInformation = new TaskDeploymentDescriptor.NonOffloaded<>(jobInformationOrBlobKey.left());
} else {
serializedJobInformation = new TaskDeploymentDescriptor.Offloaded<>(jobInformationOrBlobKey.right());
}
final Either<SerializedValue<TaskInformation>, PermanentBlobKey> taskInformationOrBlobKey;
try {
taskInformationOrBlobKey = jobVertex.getTaskInformationOrBlobKey();
} catch (IOException e) {
throw new ExecutionGraphException(
"Could not create a serialized JobVertexInformation for " +
jobVertex.getJobVertexId(), e);
}
final TaskDeploymentDescriptor.MaybeOffloaded<TaskInformation> serializedTaskInformation;
if (taskInformationOrBlobKey.isLeft()) {
serializedTaskInformation = new TaskDeploymentDescriptor.NonOffloaded<>(taskInformationOrBlobKey.left());
} else {
serializedTaskInformation = new TaskDeploymentDescriptor.Offloaded<>(taskInformationOrBlobKey.right());
}
return new TaskDeploymentDescriptor(
getJobId(),
serializedJobInformation,
serializedTaskInformation,
executionId,
targetSlot.getAllocationId(),
subTaskIndex,
attemptNumber,
targetSlot.getPhysicalSlotNumber(),
taskRestore,
producedPartitions,
consumedPartitions);
} | Creates a task deployment descriptor to deploy a subtask to the given target slot.
TODO: This should actually be in the EXECUTION |
public void resumeTransaction(long producerId, short epoch) {
Preconditions.checkState(producerId >= 0 && epoch >= 0, "Incorrect values for producerId {} and epoch {}", producerId, epoch);
LOG.info("Attempting to resume transaction {} with producerId {} and epoch {}", transactionalId, producerId, epoch);
Object transactionManager = getValue(kafkaProducer, "transactionManager");
synchronized (transactionManager) {
Object sequenceNumbers = getValue(transactionManager, "sequenceNumbers");
invoke(transactionManager, "transitionTo", getEnum("org.apache.kafka.clients.producer.internals.TransactionManager$State.INITIALIZING"));
invoke(sequenceNumbers, "clear");
Object producerIdAndEpoch = getValue(transactionManager, "producerIdAndEpoch");
setValue(producerIdAndEpoch, "producerId", producerId);
setValue(producerIdAndEpoch, "epoch", epoch);
invoke(transactionManager, "transitionTo", getEnum("org.apache.kafka.clients.producer.internals.TransactionManager$State.READY"));
invoke(transactionManager, "transitionTo", getEnum("org.apache.kafka.clients.producer.internals.TransactionManager$State.IN_TRANSACTION"));
setValue(transactionManager, "transactionStarted", true);
}
} | Instead of obtaining producerId and epoch from the transaction coordinator, re-use previously obtained ones,
so that we can resume transaction after a restart. Implementation of this method is based on
{@link org.apache.kafka.clients.producer.KafkaProducer#initTransactions}. |
private void flushNewPartitions() {
LOG.info("Flushing new partitions");
TransactionalRequestResult result = enqueueNewPartitions();
Object sender = getValue(kafkaProducer, "sender");
invoke(sender, "wakeup");
result.await();
} | Besides committing {@link org.apache.kafka.clients.producer.KafkaProducer#commitTransaction} is also adding new
partitions to the transaction. flushNewPartitions method is moving this logic to pre-commit/flush, to make
resumeTransaction simpler. Otherwise resumeTransaction would require to restore state of the not yet added/"in-flight"
partitions. |
private static void getContainedGenericTypes(CompositeType<?> typeInfo, List<GenericTypeInfo<?>> target) {
for (int i = 0; i < typeInfo.getArity(); i++) {
TypeInformation<?> type = typeInfo.getTypeAt(i);
if (type instanceof CompositeType) {
getContainedGenericTypes((CompositeType<?>) type, target);
} else if (type instanceof GenericTypeInfo) {
if (!target.contains(type)) {
target.add((GenericTypeInfo<?>) type);
}
}
}
} | Returns all GenericTypeInfos contained in a composite type.
@param typeInfo {@link CompositeType} |
@Override
public void close() throws Exception {
synchronized (lock) {
if (!isShutDown()) {
// stop all job manager leader services
for (EmbeddedLeaderService service : jobManagerLeaderServices.values()) {
service.shutdown();
}
jobManagerLeaderServices.clear();
resourceManagerLeaderService.shutdown();
webMonitorLeaderService.shutdown();
}
super.close();
}
} | ------------------------------------------------------------------------ |
@Override
protected void initialize() throws Exception {
super.initialize();
// sanity check: the tail has to update either the workset or the solution set
if (!isWorksetUpdate && !isSolutionSetUpdate) {
throw new RuntimeException("The iteration tail doesn't update workset or the solution set.");
}
// set the last output collector of this task to reflect the iteration tail state update:
// a) workset update,
// b) solution set update, or
// c) merged workset and solution set update
Collector<OT> outputCollector = null;
if (isWorksetUpdate) {
outputCollector = createWorksetUpdateOutputCollector();
// we need the WorksetUpdateOutputCollector separately to count the collected elements
if (isWorksetIteration) {
worksetUpdateOutputCollector = (WorksetUpdateOutputCollector<OT>) outputCollector;
}
}
if (isSolutionSetUpdate) {
if (isWorksetIteration) {
outputCollector = createSolutionSetUpdateOutputCollector(outputCollector);
}
// Bulk iteration with termination criterion
else {
outputCollector = new Collector<OT>() {
@Override
public void collect(OT record) {}
@Override
public void close() {}
};
}
if (!isWorksetUpdate) {
solutionSetUpdateBarrier = SolutionSetUpdateBarrierBroker.instance().get(brokerKey());
}
}
setLastOutputCollector(outputCollector);
} | -------------------------------------------------------------------------------------------- |
@Override
public RefCountedFile apply(File file) throws IOException {
final File directory = tempDirectories[nextIndex()];
while (true) {
try {
if (file == null) {
final File newFile = new File(directory, ".tmp_" + UUID.randomUUID());
final OutputStream out = Files.newOutputStream(newFile.toPath(), StandardOpenOption.CREATE_NEW);
return RefCountedFile.newFile(newFile, out);
} else {
final OutputStream out = Files.newOutputStream(file.toPath(), StandardOpenOption.APPEND);
return RefCountedFile.restoredFile(file, out, file.length());
}
} catch (FileAlreadyExistsException ignored) {
// fall through the loop and retry
}
}
} | Gets the next temp file and stream to temp file.
This creates the temp file atomically, making sure no previous file is overwritten.
<p>This method is safe against concurrent use.
@return A pair of temp file and output stream to that temp file.
@throws IOException Thrown, if the stream to the temp file could not be opened. |
@SuppressWarnings("unchecked")
private static DataSet<StringTriple> getDataSet(ExecutionEnvironment env, ParameterTool params) {
if (params.has("input")) {
return env.readCsvFile(params.get("input"))
.fieldDelimiter(";")
.pojoType(StringTriple.class);
} else {
System.out.println("Executing EmptyFieldsCountAccumulator example with default input data set.");
System.out.println("Use --input to specify file input.");
return env.fromCollection(getExampleInputTuples());
}
} | ************************************************************************* |
public <KEY> Where<KEY> where(KeySelector<T1, KEY> keySelector) {
Preconditions.checkNotNull(keySelector);
final TypeInformation<KEY> keyType = TypeExtractor.getKeySelectorTypes(keySelector, input1.getType());
return where(keySelector, keyType);
} | Specifies a {@link KeySelector} for elements from the first input.
@param keySelector The KeySelector to be used for extracting the first input's key for partitioning. |
public <KEY> Where<KEY> where(KeySelector<T1, KEY> keySelector, TypeInformation<KEY> keyType) {
Preconditions.checkNotNull(keySelector);
Preconditions.checkNotNull(keyType);
return new Where<>(input1.clean(keySelector), keyType);
} | Specifies a {@link KeySelector} for elements from the first input with explicit type information.
@param keySelector The KeySelector to be used for extracting the first input's key for partitioning.
@param keyType The type information describing the key type. |
final protected void handleProcessedBuffer(T buffer, IOException ex) {
if (buffer == null) {
return;
}
// even if the callbacks throw an error, we need to maintain our bookkeeping
try {
if (ex != null && this.exception == null) {
this.exception = ex;
this.resultHandler.requestFailed(buffer, ex);
}
else {
this.resultHandler.requestSuccessful(buffer);
}
}
finally {
NotificationListener listener = null;
// Decrement the number of outstanding requests. If we are currently closing, notify the
// waiters. If there is a listener, notify her as well.
synchronized (this.closeLock) {
if (this.requestsNotReturned.decrementAndGet() == 0) {
if (this.closed) {
this.closeLock.notifyAll();
}
synchronized (listenerLock) {
listener = allRequestsProcessedListener;
allRequestsProcessedListener = null;
}
}
}
if (listener != null) {
listener.onNotification();
}
}
} | Handles a processed <tt>Buffer</tt>. This method is invoked by the
asynchronous IO worker threads upon completion of the IO request with the
provided buffer and/or an exception that occurred while processing the request
for that buffer.
@param buffer The buffer to be processed.
@param ex The exception that occurred in the I/O threads when processing the buffer's request. |
protected boolean registerAllRequestsProcessedListener(NotificationListener listener) throws IOException {
checkNotNull(listener);
synchronized (listenerLock) {
if (allRequestsProcessedListener == null) {
// There was a race with the processing of the last outstanding request
if (requestsNotReturned.get() == 0) {
return false;
}
allRequestsProcessedListener = listener;
return true;
}
}
throw new IllegalStateException("Already subscribed.");
} | Registers a listener to be notified when all outstanding requests have been processed.
<p> New requests can arrive right after the listener got notified. Therefore, it is not safe
to assume that the number of outstanding requests is still zero after a notification unless
there was a close right before the listener got called.
<p> Returns <code>true</code>, if the registration was successful. A registration can fail,
if there are no outstanding requests when trying to register a listener. |
public static TumblingEventTimeWindows of(Time size, Time offset) {
return new TumblingEventTimeWindows(size.toMilliseconds(), offset.toMilliseconds());
} | Creates a new {@code TumblingEventTimeWindows} {@link WindowAssigner} that assigns
elements to time windows based on the element timestamp and offset.
<p>For example, if you want window a stream by hour,but window begins at the 15th minutes
of each hour, you can use {@code of(Time.hours(1),Time.minutes(15))},then you will get
time windows start at 0:15:00,1:15:00,2:15:00,etc.
<p>Rather than that,if you are living in somewhere which is not using UTC±00:00 time,
such as China which is using UTC+08:00,and you want a time window with size of one day,
and window begins at every 00:00:00 of local time,you may use {@code of(Time.days(1),Time.hours(-8))}.
The parameter of offset is {@code Time.hours(-8))} since UTC+08:00 is 8 hours earlier than UTC time.
@param size The size of the generated windows.
@param offset The offset which window start would be shifted by.
@return The time policy. |
@Override
public DataSet<Vertex<K, VV>> createResult() {
if (this.initialVertices == null) {
throw new IllegalStateException("The input data set has not been set.");
}
// prepare some type information
TypeInformation<K> keyType = ((TupleTypeInfo<?>) initialVertices.getType()).getTypeAt(0);
TypeInformation<Tuple2<K, Message>> messageTypeInfo = new TupleTypeInfo<>(keyType, messageType);
// create a graph
Graph<K, VV, EV> graph =
Graph.fromDataSet(initialVertices, edgesWithValue, initialVertices.getExecutionEnvironment());
// check whether the numVertices option is set and, if so, compute the total number of vertices
// and set it within the scatter and gather functions
DataSet<LongValue> numberOfVertices = null;
if (this.configuration != null && this.configuration.isOptNumVertices()) {
try {
numberOfVertices = GraphUtils.count(this.initialVertices);
} catch (Exception e) {
e.printStackTrace();
}
}
if (this.configuration != null) {
scatterFunction.setDirection(this.configuration.getDirection());
} else {
scatterFunction.setDirection(EdgeDirection.OUT);
}
// retrieve the direction in which the updates are made and in which the messages are sent
EdgeDirection messagingDirection = scatterFunction.getDirection();
// check whether the degrees option is set and, if so, compute the in and the out degrees and
// add them to the vertex value
if (this.configuration != null && this.configuration.isOptDegrees()) {
return createResultVerticesWithDegrees(graph, messagingDirection, messageTypeInfo, numberOfVertices);
} else {
return createResultSimpleVertex(messagingDirection, messageTypeInfo, numberOfVertices);
}
} | Creates the operator that represents this scatter-gather graph computation.
@return The operator that represents this scatter-gather graph computation. |
public static <K, VV, Message, EV> ScatterGatherIteration<K, VV, Message, EV> withEdges(
DataSet<Edge<K, EV>> edgesWithValue, ScatterFunction<K, VV, Message, EV> sf,
GatherFunction<K, VV, Message> gf, int maximumNumberOfIterations) {
return new ScatterGatherIteration<>(sf, gf, edgesWithValue, maximumNumberOfIterations);
} | Creates a new scatter-gather iteration operator for graphs where the edges are associated with a value (such as
a weight or distance).
@param edgesWithValue The data set containing edges.
@param sf The function that turns changed vertex states into messages along the edges.
@param gf The function that updates the state of the vertices from the incoming messages.
@param <K> The type of the vertex key (the vertex identifier).
@param <VV> The type of the vertex value (the state of the vertex).
@param <Message> The type of the message sent between vertices along the edges.
@param <EV> The type of the values that are associated with the edges.
@return An in stance of the scatter-gather graph computation operator. |
private CoGroupOperator<?, ?, Tuple2<K, Message>> buildScatterFunctionVerticesWithDegrees(
DeltaIteration<Vertex<K, Tuple3<VV, LongValue, LongValue>>, Vertex<K, Tuple3<VV, LongValue, LongValue>>> iteration,
TypeInformation<Tuple2<K, Message>> messageTypeInfo, int whereArg, int equalToArg,
DataSet<LongValue> numberOfVertices) {
// build the scatter function (co group)
CoGroupOperator<?, ?, Tuple2<K, Message>> messages;
ScatterUdfWithEdgeValues<K, Tuple3<VV, LongValue, LongValue>, VV, Message, EV> messenger =
new ScatterUdfWithEVsVVWithDegrees<>(scatterFunction, messageTypeInfo);
messages = this.edgesWithValue.coGroup(iteration.getWorkset()).where(whereArg)
.equalTo(equalToArg).with(messenger);
// configure coGroup message function with name and broadcast variables
messages = messages.name("Messaging");
if (this.configuration != null) {
for (Tuple2<String, DataSet<?>> e : this.configuration.getScatterBcastVars()) {
messages = messages.withBroadcastSet(e.f1, e.f0);
}
if (this.configuration.isOptNumVertices()) {
messages = messages.withBroadcastSet(numberOfVertices, "number of vertices");
}
}
return messages;
} | Method that builds the scatter function using a coGroup operator for a vertex
containing degree information.
It afterwards configures the function with a custom name and broadcast variables.
@param iteration
@param messageTypeInfo
@param whereArg the argument for the where within the coGroup
@param equalToArg the argument for the equalTo within the coGroup
@return the scatter function |
private DataSet<Vertex<K, VV>> createResultSimpleVertex(EdgeDirection messagingDirection,
TypeInformation<Tuple2<K, Message>> messageTypeInfo, DataSet<LongValue> numberOfVertices) {
DataSet<Tuple2<K, Message>> messages;
TypeInformation<Vertex<K, VV>> vertexTypes = initialVertices.getType();
final DeltaIteration<Vertex<K, VV>, Vertex<K, VV>> iteration =
initialVertices.iterateDelta(initialVertices, this.maximumNumberOfIterations, 0);
setUpIteration(iteration);
switch (messagingDirection) {
case IN:
messages = buildScatterFunction(iteration, messageTypeInfo, 1, 0, numberOfVertices);
break;
case OUT:
messages = buildScatterFunction(iteration, messageTypeInfo, 0, 0, numberOfVertices);
break;
case ALL:
messages = buildScatterFunction(iteration, messageTypeInfo, 1, 0, numberOfVertices)
.union(buildScatterFunction(iteration, messageTypeInfo, 0, 0, numberOfVertices));
break;
default:
throw new IllegalArgumentException("Illegal edge direction");
}
GatherUdf<K, VV, Message> updateUdf = new GatherUdfSimpleVV<>(gatherFunction, vertexTypes);
// build the update function (co group)
CoGroupOperator<?, ?, Vertex<K, VV>> updates =
messages.coGroup(iteration.getSolutionSet()).where(0).equalTo(0).with(updateUdf);
if (this.configuration != null && this.configuration.isOptNumVertices()) {
updates = updates.withBroadcastSet(numberOfVertices, "number of vertices");
}
configureUpdateFunction(updates);
return iteration.closeWith(updates, updates);
} | Creates the operator that represents this scatter-gather graph computation for a simple vertex.
@param messagingDirection
@param messageTypeInfo
@param numberOfVertices
@return the operator |
@SuppressWarnings("serial")
private DataSet<Vertex<K, VV>> createResultVerticesWithDegrees(Graph<K, VV, EV> graph, EdgeDirection messagingDirection,
TypeInformation<Tuple2<K, Message>> messageTypeInfo, DataSet<LongValue> numberOfVertices) {
DataSet<Tuple2<K, Message>> messages;
this.gatherFunction.setOptDegrees(this.configuration.isOptDegrees());
DataSet<Tuple2<K, LongValue>> inDegrees = graph.inDegrees();
DataSet<Tuple2<K, LongValue>> outDegrees = graph.outDegrees();
DataSet<Tuple3<K, LongValue, LongValue>> degrees = inDegrees.join(outDegrees).where(0).equalTo(0)
.with(new FlatJoinFunction<Tuple2<K, LongValue>, Tuple2<K, LongValue>, Tuple3<K, LongValue, LongValue>>() {
@Override
public void join(Tuple2<K, LongValue> first, Tuple2<K, LongValue> second, Collector<Tuple3<K, LongValue, LongValue>> out) {
out.collect(new Tuple3<>(first.f0, first.f1, second.f1));
}
}).withForwardedFieldsFirst("f0;f1").withForwardedFieldsSecond("f1");
DataSet<Vertex<K, Tuple3<VV, LongValue, LongValue>>> verticesWithDegrees = initialVertices
.join(degrees).where(0).equalTo(0)
.with(new FlatJoinFunction<Vertex<K, VV>, Tuple3<K, LongValue, LongValue>, Vertex<K, Tuple3<VV, LongValue, LongValue>>>() {
@Override
public void join(Vertex<K, VV> vertex, Tuple3<K, LongValue, LongValue> degrees,
Collector<Vertex<K, Tuple3<VV, LongValue, LongValue>>> out) throws Exception {
out.collect(new Vertex<>(vertex.getId(),
new Tuple3<>(vertex.getValue(), degrees.f1, degrees.f2)));
}
}).withForwardedFieldsFirst("f0");
// add type info
TypeInformation<Vertex<K, Tuple3<VV, LongValue, LongValue>>> vertexTypes = verticesWithDegrees.getType();
final DeltaIteration<Vertex<K, Tuple3<VV, LongValue, LongValue>>, Vertex<K, Tuple3<VV, LongValue, LongValue>>> iteration =
verticesWithDegrees.iterateDelta(verticesWithDegrees, this.maximumNumberOfIterations, 0);
setUpIteration(iteration);
switch (messagingDirection) {
case IN:
messages = buildScatterFunctionVerticesWithDegrees(iteration, messageTypeInfo, 1, 0, numberOfVertices);
break;
case OUT:
messages = buildScatterFunctionVerticesWithDegrees(iteration, messageTypeInfo, 0, 0, numberOfVertices);
break;
case ALL:
messages = buildScatterFunctionVerticesWithDegrees(iteration, messageTypeInfo, 1, 0, numberOfVertices)
.union(buildScatterFunctionVerticesWithDegrees(iteration, messageTypeInfo, 0, 0, numberOfVertices));
break;
default:
throw new IllegalArgumentException("Illegal edge direction");
}
@SuppressWarnings({ "unchecked", "rawtypes" })
GatherUdf<K, Tuple3<VV, LongValue, LongValue>, Message> updateUdf =
new GatherUdfVVWithDegrees(gatherFunction, vertexTypes);
// build the update function (co group)
CoGroupOperator<?, ?, Vertex<K, Tuple3<VV, LongValue, LongValue>>> updates =
messages.coGroup(iteration.getSolutionSet()).where(0).equalTo(0).with(updateUdf);
if (this.configuration != null && this.configuration.isOptNumVertices()) {
updates = updates.withBroadcastSet(numberOfVertices, "number of vertices");
}
configureUpdateFunction(updates);
return iteration.closeWith(updates, updates).map(
new MapFunction<Vertex<K, Tuple3<VV, LongValue, LongValue>>, Vertex<K, VV>>() {
public Vertex<K, VV> map(Vertex<K, Tuple3<VV, LongValue, LongValue>> vertex) {
return new Vertex<>(vertex.getId(), vertex.getValue().f0);
}
});
} | Creates the operator that represents this scatter-gather graph computation for a vertex with in
and out degrees added to the vertex value.
@param graph
@param messagingDirection
@param messageTypeInfo
@param numberOfVertices
@return the operator |
public static DataSet<Vertex<Long, String>> getVertices(ExecutionEnvironment env) {
List<Vertex<Long, String>> vertices = new ArrayList<>(INPUT_VERTICES.length);
for (String vertex : INPUT_VERTICES) {
String[] tokens = vertex.split(";");
vertices.add(new Vertex<>(Long.parseLong(tokens[0]), tokens[1]));
}
return env.fromCollection(vertices);
} | Creates a set of vertices with attached {@link String} values.
@param env execution environment
@return vertex data set with string values |
public static DataSet<Edge<Long, String>> getEdges(ExecutionEnvironment env) {
List<Edge<Long, String>> edges = new ArrayList<>(INPUT_EDGES.length);
for (String edge : INPUT_EDGES) {
String[] tokens = edge.split(";");
edges.add(new Edge<>(Long.parseLong(tokens[0]), Long.parseLong(tokens[1]), tokens[2]));
}
return env.fromCollection(edges);
} | Creates a set of edges with attached {@link String} values.
@param env execution environment
@return edge data set with string values |
@Override
protected List<OUT> executeOnCollections(List<IN> input, RuntimeContext ctx, ExecutionConfig executionConfig) throws Exception {
FlatMapFunction<IN, OUT> function = userFunction.getUserCodeObject();
FunctionUtils.setFunctionRuntimeContext(function, ctx);
FunctionUtils.openFunction(function, parameters);
ArrayList<OUT> result = new ArrayList<OUT>(input.size());
TypeSerializer<IN> inSerializer = getOperatorInfo().getInputType().createSerializer(executionConfig);
TypeSerializer<OUT> outSerializer = getOperatorInfo().getOutputType().createSerializer(executionConfig);
CopyingListCollector<OUT> resultCollector = new CopyingListCollector<OUT>(result, outSerializer);
for (IN element : input) {
IN inCopy = inSerializer.copy(element);
function.flatMap(inCopy, resultCollector);
}
FunctionUtils.closeFunction(function);
return result;
} | ------------------------------------------------------------------------ |
public O name(String newName) {
this.name = newName;
@SuppressWarnings("unchecked")
O returnType = (O) this;
return returnType;
} | Sets the name of this operator. This overrides the default name, which is either
a generated description of the operation (such as for example "Aggregate(1:SUM, 2:MIN)")
or the name the user-defined function or input/output format executed by the operator.
@param newName The name for this operator.
@return The operator with a new name. |
public O setParallelism(int parallelism) {
Preconditions.checkArgument(parallelism > 0 || parallelism == ExecutionConfig.PARALLELISM_DEFAULT,
"The parallelism must be at least one, or ExecutionConfig.PARALLELISM_DEFAULT (use system default).");
this.parallelism = parallelism;
@SuppressWarnings("unchecked")
O returnType = (O) this;
return returnType;
} | Sets the parallelism for this operator.
The parallelism must be 1 or more.
@param parallelism The parallelism for this operator. A value equal to {@link ExecutionConfig#PARALLELISM_DEFAULT}
will use the system default.
@return The operator with set parallelism. |
private O setResources(ResourceSpec minResources, ResourceSpec preferredResources) {
Preconditions.checkNotNull(minResources, "The min resources must be not null.");
Preconditions.checkNotNull(preferredResources, "The preferred resources must be not null.");
Preconditions.checkArgument(minResources.isValid() && preferredResources.isValid() && minResources.lessThanOrEqual(preferredResources),
"The values in resources must be not less than 0 and the preferred resources must be greater than the min resources.");
this.minResources = minResources;
this.preferredResources = preferredResources;
@SuppressWarnings("unchecked")
O returnType = (O) this;
return returnType;
} | Sets the minimum and preferred resources for this operator. This overrides the default resources.
The lower and upper resource limits will be considered in dynamic resource resize feature for future plan.
@param minResources The minimum resources for this operator.
@param preferredResources The preferred resources for this operator.
@return The operator with set minimum and preferred resources. |
private O setResources(ResourceSpec resources) {
Preconditions.checkNotNull(resources, "The resources must be not null.");
Preconditions.checkArgument(resources.isValid(), "The values in resources must be not less than 0.");
this.minResources = resources;
this.preferredResources = resources;
@SuppressWarnings("unchecked")
O returnType = (O) this;
return returnType;
} | Sets the resources for this operator. This overrides the default minimum and preferred resources.
@param resources The resources for this operator.
@return The operator with set minimum and preferred resources. |
@Override
public E next(E reuse) throws IOException {
/* There are three ways to handle object reuse:
* 1) reuse and return the given object
* 2) ignore the given object and return a new object
* 3) exchange the given object for an existing object
*
* The first option is not available here as the return value has
* already been deserialized from the heap's top iterator. The second
* option avoids object reuse. The third option is implemented below
* by passing the given object to the heap's top iterator into which
* the next value will be deserialized.
*/
if (this.heap.size() > 0) {
// get the smallest element
final HeadStream<E> top = this.heap.peek();
E result = top.getHead();
// read an element
if (!top.nextHead(reuse)) {
this.heap.poll();
} else {
this.heap.adjustTop();
}
return result;
}
else {
return null;
}
} | Gets the next smallest element, with respect to the definition of order implied by
the {@link TypeSerializer} provided to this iterator.
@param reuse Object that may be reused.
@return The next element if the iterator has another element, null otherwise.
@see org.apache.flink.util.MutableObjectIterator#next(java.lang.Object) |
public GraphAlgorithmWrappingBase<K, VV, EV, R> setParallelism(int parallelism) {
Preconditions.checkArgument(parallelism > 0 || parallelism == PARALLELISM_DEFAULT,
"The parallelism must be at least one, or ExecutionConfig.PARALLELISM_DEFAULT (use system default).");
this.parallelism = parallelism;
return this;
} | Set the parallelism for this algorithm's operators. This parameter is
necessary because processing a small amount of data with high operator
parallelism is slow and wasteful with memory and buffers.
<p>Operator parallelism should be set to this given value unless
processing asymptotically more data, in which case the default job
parallelism should be inherited.
@param parallelism operator parallelism
@return this |
protected boolean canMergeConfigurationWith(GraphAlgorithmWrappingBase other) {
Preconditions.checkNotNull(other);
return this.getClass().equals(other.getClass());
} | First test whether the algorithm configurations can be merged before the
call to {@link #mergeConfiguration}.
@param other the algorithm with which to compare configuration
@return true if and only if configuration can be merged and the
algorithm's output can be reused
@see #mergeConfiguration(GraphAlgorithmWrappingBase) |
protected void mergeConfiguration(GraphAlgorithmWrappingBase other) {
Preconditions.checkNotNull(other);
parallelism = (parallelism == PARALLELISM_DEFAULT) ? other.parallelism :
((other.parallelism == PARALLELISM_DEFAULT) ? parallelism : Math.min(parallelism, other.parallelism));
} | Merge the other configuration into this algorithm's after the call to
{@link #canMergeConfigurationWith} has checked that the configurations
can be merged.
@param other the algorithm from which to merge configuration
@see #canMergeConfigurationWith(GraphAlgorithmWrappingBase) |
public boolean nextKey() throws IOException {
if (lookahead != null) {
// common case: whole value-iterator was consumed and a new key group is available.
this.comparator.setReference(this.lookahead);
this.valuesIterator.next = this.lookahead;
this.lastKeyRecord = this.lookahead;
this.lookahead = null;
this.valuesIterator.iteratorAvailable = true;
return true;
}
// first element, empty/done, or the values iterator was not entirely consumed
if (this.done) {
return false;
}
if (this.valuesIterator != null) {
// values was not entirely consumed. move to the next key
// Required if user code / reduce() method did not read the whole value iterator.
E next;
while (true) {
if ((next = this.iterator.next()) != null) {
if (!this.comparator.equalToReference(next)) {
// the keys do not match, so we have a new group. store the current key
this.comparator.setReference(next);
this.valuesIterator.next = next;
this.lastKeyRecord = next;
this.valuesIterator.iteratorAvailable = true;
return true;
}
}
else {
// input exhausted
this.valuesIterator.next = null;
this.valuesIterator = null;
this.lastKeyRecord = null;
this.done = true;
return false;
}
}
}
else {
// first element
// get the next element
E first = this.iterator.next();
if (first != null) {
this.comparator.setReference(first);
this.valuesIterator = new ValuesIterator(first);
this.lastKeyRecord = first;
return true;
}
else {
// empty input, set everything null
this.done = true;
return false;
}
}
} | Moves the iterator to the next key. This method may skip any values that have not yet been returned by the
iterator created by the {@link #getValues()} method. Hence, if called multiple times it "removes" key groups.
@return true, if the input iterator has an other group of records with the same key. |
@Override
public T deserialize(byte[] message) {
if (dis != null) {
dis.setBuffer(message);
} else {
dis = new DataInputDeserializer(message);
}
try {
return serializer.deserialize(dis);
}
catch (IOException e) {
throw new RuntimeException("Unable to deserialize message", e);
}
} | ------------------------------------------------------------------------ |
public int getNumberOfAvailableSlotsForGroup(AbstractID groupId) {
synchronized (lock) {
Map<ResourceID, List<SharedSlot>> available = availableSlotsPerJid.get(groupId);
if (available != null) {
Set<SharedSlot> set = new HashSet<SharedSlot>();
for (List<SharedSlot> list : available.values()) {
for (SharedSlot slot : list) {
set.add(slot);
}
}
return set.size();
}
else {
// if no entry exists for a JobVertexID so far, then the vertex with that ID can
// add a subtask into each shared slot of this group. Consequently, all
// of them are available for that JobVertexID.
return allSlots.size();
}
}
} | Gets the number of shared slots into which the given group can place subtasks or
nested task groups.
@param groupId The ID of the group.
@return The number of shared slots available to the given job vertex. |
public SimpleSlot addSharedSlotAndAllocateSubSlot(SharedSlot sharedSlot, Locality locality, JobVertexID groupId) {
return addSharedSlotAndAllocateSubSlot(sharedSlot, locality, groupId, null);
} | ------------------------------------------------------------------------ |
public SimpleSlot getSlotForTask(JobVertexID vertexID, Iterable<TaskManagerLocation> locationPreferences) {
synchronized (lock) {
Tuple2<SharedSlot, Locality> p = getSharedSlotForTask(vertexID, locationPreferences, false);
if (p != null) {
SharedSlot ss = p.f0;
SimpleSlot slot = ss.allocateSubSlot(vertexID);
slot.setLocality(p.f1);
return slot;
}
else {
return null;
}
}
} | Gets a slot suitable for the given task vertex. This method will prefer slots that are local
(with respect to {@link ExecutionVertex#getPreferredLocationsBasedOnInputs()}), but will return non local
slots if no local slot is available. The method returns null, when this sharing group has
no slot available for the given JobVertexID.
@param vertexID the vertex id
@param locationPreferences location preferences
@return A slot to execute the given ExecutionVertex in, or null, if none is available. |
public SimpleSlot getSlotForTask(CoLocationConstraint constraint, Iterable<TaskManagerLocation> locationPreferences) {
synchronized (lock) {
if (constraint.isAssignedAndAlive()) {
// the shared slot of the co-location group is initialized and set we allocate a sub-slot
final SharedSlot shared = constraint.getSharedSlot();
SimpleSlot subslot = shared.allocateSubSlot(null);
subslot.setLocality(Locality.LOCAL);
return subslot;
}
else if (constraint.isAssigned()) {
// we had an assignment before.
SharedSlot previous = constraint.getSharedSlot();
if (previous == null) {
throw new IllegalStateException("Bug: Found assigned co-location constraint without a slot.");
}
TaskManagerLocation location = previous.getTaskManagerLocation();
Tuple2<SharedSlot, Locality> p = getSharedSlotForTask(
constraint.getGroupId(), Collections.singleton(location), true);
if (p == null) {
return null;
}
else {
SharedSlot newSharedSlot = p.f0;
// allocate the co-location group slot inside the shared slot
SharedSlot constraintGroupSlot = newSharedSlot.allocateSharedSlot(constraint.getGroupId());
if (constraintGroupSlot != null) {
constraint.setSharedSlot(constraintGroupSlot);
// the sub slots in the co location constraint slot have no group that they belong to
// (other than the co-location-constraint slot)
SimpleSlot subSlot = constraintGroupSlot.allocateSubSlot(null);
subSlot.setLocality(Locality.LOCAL);
return subSlot;
}
else {
// could not allocate the co-location-constraint shared slot
return null;
}
}
}
else {
// the location constraint has not been associated with a shared slot, yet.
// grab a new slot and initialize the constraint with that one.
// preferred locations are defined by the vertex
Tuple2<SharedSlot, Locality> p =
getSharedSlotForTask(constraint.getGroupId(), locationPreferences, false);
if (p == null) {
// could not get a shared slot for this co-location-group
return null;
}
else {
final SharedSlot availableShared = p.f0;
final Locality l = p.f1;
// allocate the co-location group slot inside the shared slot
SharedSlot constraintGroupSlot = availableShared.allocateSharedSlot(constraint.getGroupId());
// IMPORTANT: We do not lock the location, yet, since we cannot be sure that the
// caller really sticks with the slot we picked!
constraint.setSharedSlot(constraintGroupSlot);
// the sub slots in the co location constraint slot have no group that they belong to
// (other than the co-location-constraint slot)
SimpleSlot sub = constraintGroupSlot.allocateSubSlot(null);
sub.setLocality(l);
return sub;
}
}
}
} | Gets a slot for a task that has a co-location constraint. This method tries to grab
a slot form the location-constraint's shared slot. If that slot has not been initialized,
then the method tries to grab another slot that is available for the location-constraint-group.
<p>In cases where the co-location constraint has not yet been initialized with a slot,
or where that slot has been disposed in the meantime, this method tries to allocate a shared
slot for the co-location constraint (inside on of the other available slots).</p>
<p>If a suitable shared slot is available, this method allocates a simple slot within that
shared slot and returns it. If no suitable shared slot could be found, this method
returns null.</p>
@param constraint The co-location constraint for the placement of the execution vertex.
@param locationPreferences location preferences
@return A simple slot allocate within a suitable shared slot, or {@code null}, if no suitable
shared slot is available. |
void releaseSimpleSlot(SimpleSlot simpleSlot) {
synchronized (lock) {
// try to transition to the CANCELED state. That state marks
// that the releasing is in progress
if (simpleSlot.markCancelled()) {
// sanity checks
if (simpleSlot.isAlive()) {
throw new IllegalStateException("slot is still alive");
}
// check whether the slot is already released
if (simpleSlot.markReleased()) {
LOG.debug("Release simple slot {}.", simpleSlot);
AbstractID groupID = simpleSlot.getGroupID();
SharedSlot parent = simpleSlot.getParent();
// if we have a group ID, then our parent slot is tracked here
if (groupID != null && !allSlots.contains(parent)) {
throw new IllegalArgumentException("Slot was not associated with this SlotSharingGroup before.");
}
int parentRemaining = parent.removeDisposedChildSlot(simpleSlot);
if (parentRemaining > 0) {
// the parent shared slot is still alive. make sure we make it
// available again to the group of the just released slot
if (groupID != null) {
// if we have a group ID, then our parent becomes available
// for that group again. otherwise, the slot is part of a
// co-location group and nothing becomes immediately available
Map<ResourceID, List<SharedSlot>> slotsForJid = availableSlotsPerJid.get(groupID);
// sanity check
if (slotsForJid == null) {
throw new IllegalStateException("Trying to return a slot for group " + groupID +
" when available slots indicated that all slots were available.");
}
putIntoMultiMap(slotsForJid, parent.getTaskManagerID(), parent);
}
} else {
// the parent shared slot is now empty and can be released
parent.markCancelled();
internalDisposeEmptySharedSlot(parent);
}
}
}
}
} | Releases the simple slot from the assignment group.
@param simpleSlot The SimpleSlot to be released |
void releaseSharedSlot(SharedSlot sharedSlot) {
synchronized (lock) {
if (sharedSlot.markCancelled()) {
// we are releasing this slot
if (sharedSlot.hasChildren()) {
final FlinkException cause = new FlinkException("Releasing shared slot parent.");
// by simply releasing all children, we should eventually release this slot.
Set<Slot> children = sharedSlot.getSubSlots();
while (children.size() > 0) {
children.iterator().next().releaseSlot(cause);
}
}
else {
// if there are no children that trigger the release, we trigger it directly
internalDisposeEmptySharedSlot(sharedSlot);
}
}
}
} | Called from {@link org.apache.flink.runtime.instance.SharedSlot#releaseSlot(Throwable)}.
@param sharedSlot The slot to be released. |
private static void putIntoMultiMap(Map<ResourceID, List<SharedSlot>> map, ResourceID location, SharedSlot slot) {
List<SharedSlot> slotsForInstance = map.get(location);
if (slotsForInstance == null) {
slotsForInstance = new ArrayList<SharedSlot>();
map.put(location, slotsForInstance);
}
slotsForInstance.add(slot);
} | ------------------------------------------------------------------------ |
public int[] toArray() {
int[] a = new int[this.collection.size()];
int i = 0;
for (int col : this.collection) {
a[i++] = col;
}
return a;
} | Transforms the field set into an array of field IDs. Whether the IDs are ordered
or unordered depends on the specific subclass of the field set.
@return An array of all contained field IDs. |
public boolean isValidSubset(FieldSet set) {
if (set.size() > size()) {
return false;
}
for (Integer i : set) {
if (!contains(i)) {
return false;
}
}
return true;
} | Checks if the given set of fields is a valid subset of this set of fields. For unordered
sets, this is the case if all of the given set's fields are also part of this field.
<p>
Subclasses that describe field sets where the field order matters must override this method
to implement a field ordering sensitive check.
@param set The set that is a candidate subset.
@return True, if the given set is a subset of this set, false otherwise. |
public AdamicAdar<K, VV, EV> setMinimumScore(float score) {
Preconditions.checkArgument(score >= 0, "Minimum score must be non-negative");
this.minimumScore = score;
return this;
} | Filter out Adamic-Adar scores less than the given minimum.
@param score minimum score
@return this |
public AdamicAdar<K, VV, EV> setMinimumRatio(float ratio) {
Preconditions.checkArgument(ratio >= 0, "Minimum ratio must be non-negative");
this.minimumRatio = ratio;
return this;
} | Filter out Adamic-Adar scores less than the given ratio times the average score.
@param ratio minimum ratio
@return this |
@Override
public DataSet<Result<K>> runInternal(Graph<K, VV, EV> input)
throws Exception {
// s, d(s), 1/log(d(s))
DataSet<Tuple3<K, LongValue, FloatValue>> inverseLogDegree = input
.run(new VertexDegree<K, VV, EV>()
.setParallelism(parallelism))
.map(new VertexInverseLogDegree<>())
.setParallelism(parallelism)
.name("Vertex score");
// s, t, 1/log(d(s))
DataSet<Tuple3<K, K, FloatValue>> sourceInverseLogDegree = input
.getEdges()
.join(inverseLogDegree, JoinHint.REPARTITION_HASH_SECOND)
.where(0)
.equalTo(0)
.projectFirst(0, 1)
.<Tuple3<K, K, FloatValue>>projectSecond(2)
.setParallelism(parallelism)
.name("Edge score");
// group span, s, t, 1/log(d(s))
DataSet<Tuple4<IntValue, K, K, FloatValue>> groupSpans = sourceInverseLogDegree
.groupBy(0)
.sortGroup(1, Order.ASCENDING)
.reduceGroup(new GenerateGroupSpans<>())
.setParallelism(parallelism)
.name("Generate group spans");
// group, s, t, 1/log(d(s))
DataSet<Tuple4<IntValue, K, K, FloatValue>> groups = groupSpans
.rebalance()
.setParallelism(parallelism)
.name("Rebalance")
.flatMap(new GenerateGroups<>())
.setParallelism(parallelism)
.name("Generate groups");
// t, u, 1/log(d(s)) where (s, t) and (s, u) are edges in graph
DataSet<Tuple3<K, K, FloatValue>> twoPaths = groups
.groupBy(0, 1)
.sortGroup(2, Order.ASCENDING)
.reduceGroup(new GenerateGroupPairs<>())
.name("Generate group pairs");
// t, u, adamic-adar score
GroupReduceOperator<Tuple3<K, K, FloatValue>, Result<K>> scores = twoPaths
.groupBy(0, 1)
.reduceGroup(new ComputeScores<>(minimumScore, minimumRatio))
.name("Compute scores");
if (minimumRatio > 0.0f) {
// total score, number of pairs of neighbors
DataSet<Tuple2<FloatValue, LongValue>> sumOfScoresAndNumberOfNeighborPairs = inverseLogDegree
.map(new ComputeScoreFromVertex<>())
.setParallelism(parallelism)
.name("Average score")
.sum(0)
.andSum(1);
scores
.withBroadcastSet(sumOfScoresAndNumberOfNeighborPairs, SUM_OF_SCORES_AND_NUMBER_OF_NEIGHBOR_PAIRS);
}
if (mirrorResults) {
return scores
.flatMap(new MirrorResult<>())
.name("Mirror results");
} else {
return scores;
}
} | /*
Implementation notes:
The requirement that "K extends CopyableValue<K>" can be removed when
Flink has a self-join which performs the skew distribution handled by
GenerateGroupSpans / GenerateGroups / GenerateGroupPairs. |
@Override
public int compareTo(ValueArray<FloatValue> o) {
FloatValueArray other = (FloatValueArray) o;
int min = Math.min(position, other.position);
for (int i = 0; i < min; i++) {
int cmp = Float.compare(data[i], other.data[i]);
if (cmp != 0) {
return cmp;
}
}
return Integer.compare(position, other.position);
} | -------------------------------------------------------------------------------------------- |
public static void writeBigInteger(BigInteger record, DataOutputView target) throws IOException {
// null value support
if (record == null) {
target.writeInt(0);
return;
}
// fast paths for 0, 1, 10
// only reference equality is checked because equals would be too expensive
else if (record == BigInteger.ZERO) {
target.writeInt(1);
return;
}
else if (record == BigInteger.ONE) {
target.writeInt(2);
return;
}
else if (record == BigInteger.TEN) {
target.writeInt(3);
return;
}
// default
final byte[] bytes = record.toByteArray();
// the length we write is offset by four, because null and short-paths for ZERO, ONE, and TEN
target.writeInt(bytes.length + 4);
target.write(bytes);
} | -------------------------------------------------------------------------------------------- |
public Schema schema(TableSchema schema) {
tableSchema.clear();
lastField = null;
for (int i = 0; i < schema.getFieldCount(); i++) {
field(schema.getFieldName(i).get(), schema.getFieldType(i).get());
}
return this;
} | Sets the schema with field names and the types. Required.
<p>This method overwrites existing fields added with {@link #field(String, TypeInformation)}.
@param schema the table schema |
public Schema field(String fieldName, TypeInformation<?> fieldType) {
field(fieldName, TypeStringUtils.writeTypeInfo(fieldType));
return this;
} | Adds a field with the field name and the type information. Required.
This method can be called multiple times. The call order of this method defines
also the order of the fields in a row.
@param fieldName the field name
@param fieldType the type information of the field |
public Schema field(String fieldName, String fieldType) {
if (tableSchema.containsKey(fieldName)) {
throw new ValidationException("Duplicate field name $fieldName.");
}
LinkedHashMap<String, String> fieldProperties = new LinkedHashMap<>();
fieldProperties.put(SCHEMA_TYPE, fieldType);
tableSchema.put(fieldName, fieldProperties);
lastField = fieldName;
return this;
} | Adds a field with the field name and the type string. Required.
This method can be called multiple times. The call order of this method defines
also the order of the fields in a row.
@param fieldName the field name
@param fieldType the type string of the field |
public Schema from(String originFieldName) {
if (lastField == null) {
throw new ValidationException("No field previously defined. Use field() before.");
}
tableSchema.get(lastField).put(SCHEMA_FROM, originFieldName);
lastField = null;
return this;
} | Specifies the origin of the previously defined field. The origin field is defined by a
connector or format.
<p>E.g. field("myString", Types.STRING).from("CSV_MY_STRING")
<p>Note: Field names are matched by the exact name by default (case sensitive). |
public Schema proctime() {
if (lastField == null) {
throw new ValidationException("No field defined previously. Use field() before.");
}
tableSchema.get(lastField).put(SCHEMA_PROCTIME, "true");
lastField = null;
return this;
} | Specifies the previously defined field as a processing-time attribute.
<p>E.g. field("proctime", Types.SQL_TIMESTAMP).proctime() |
public Schema rowtime(Rowtime rowtime) {
if (lastField == null) {
throw new ValidationException("No field defined previously. Use field() before.");
}
tableSchema.get(lastField).putAll(rowtime.toProperties());
lastField = null;
return this;
} | Specifies the previously defined field as an event-time attribute.
<p>E.g. field("rowtime", Types.SQL_TIMESTAMP).rowtime(...) |
@Override
public Map<String, String> toProperties() {
DescriptorProperties properties = new DescriptorProperties();
List<Map<String, String>> subKeyValues = new ArrayList<>();
for (Map.Entry<String, LinkedHashMap<String, String>> entry : tableSchema.entrySet()) {
String name = entry.getKey();
LinkedHashMap<String, String> props = entry.getValue();
Map<String, String> map = new HashMap<>();
map.put(SCHEMA_NAME, name);
map.putAll(props);
subKeyValues.add(map);
}
properties.putIndexedVariableProperties(
SCHEMA,
subKeyValues);
return properties.asMap();
} | Converts this descriptor into a set of properties. |
private void retractRecordWithRowNumber(
SortedMap<BaseRow, Long> sortedMap, BaseRow sortKey, BaseRow inputRow, Collector<BaseRow> out)
throws Exception {
Iterator<Map.Entry<BaseRow, Long>> iterator = sortedMap.entrySet().iterator();
long curRank = 0L;
boolean findsSortKey = false;
while (iterator.hasNext() && isInRankEnd(curRank)) {
Map.Entry<BaseRow, Long> entry = iterator.next();
BaseRow key = entry.getKey();
if (!findsSortKey && key.equals(sortKey)) {
List<BaseRow> inputs = dataState.get(key);
if (inputs == null) {
// Skip the data if it's state is cleared because of state ttl.
if (lenient) {
LOG.warn(STATE_CLEARED_WARN_MSG);
} else {
throw new RuntimeException(STATE_CLEARED_WARN_MSG);
}
} else {
Iterator<BaseRow> inputIter = inputs.iterator();
while (inputIter.hasNext() && isInRankEnd(curRank)) {
curRank += 1;
BaseRow prevRow = inputIter.next();
if (!findsSortKey && equaliser.equalsWithoutHeader(prevRow, inputRow)) {
delete(out, prevRow, curRank);
curRank -= 1;
findsSortKey = true;
inputIter.remove();
} else if (findsSortKey) {
retract(out, prevRow, curRank + 1);
collect(out, prevRow, curRank);
}
}
if (inputs.isEmpty()) {
dataState.remove(key);
} else {
dataState.put(key, inputs);
}
}
} else if (findsSortKey) {
List<BaseRow> inputs = dataState.get(key);
int i = 0;
while (i < inputs.size() && isInRankEnd(curRank)) {
curRank += 1;
BaseRow prevRow = inputs.get(i);
retract(out, prevRow, curRank + 1);
collect(out, prevRow, curRank);
i++;
}
} else {
curRank += entry.getValue();
}
}
} | ------------- ROW_NUMBER------------------------------- |
@Override
public long getEstimatedOutputSize() {
long estimate = this.source.template.getEstimatedOutputSize();
return estimate < 0 ? estimate : estimate * this.replicationFactor;
} | -------------------------------------------------------------------------------------------- |
public void onComplete(
final Consumer<StreamElementQueueEntry<T>> completeFunction,
Executor executor) {
final StreamElementQueueEntry<T> thisReference = this;
getFuture().whenCompleteAsync(
// call the complete function for normal completion as well as exceptional completion
// see FLINK-6435
(value, throwable) -> completeFunction.accept(thisReference),
executor);
} | Register the given complete function to be called once this queue entry has been completed.
@param completeFunction to call when the queue entry has been completed
@param executor to run the complete function |
public static String formatSystemProperties(Configuration jvmArgs) {
StringBuilder sb = new StringBuilder();
for(Map.Entry<String,String> entry : jvmArgs.toMap().entrySet()) {
if(sb.length() > 0) {
sb.append(" ");
}
boolean quoted = entry.getValue().contains(" ");
if(quoted) {
sb.append("\"");
}
sb.append("-D").append(entry.getKey()).append('=').append(entry.getValue());
if(quoted) {
sb.append("\"");
}
}
return sb.toString();
} | Format the system properties as a shell-compatible command-line argument. |
public void registerTimeout(final K key, final long delay, final TimeUnit unit) {
Preconditions.checkState(timeoutListener != null, "The " + getClass().getSimpleName() +
" has not been started.");
if (timeouts.containsKey(key)) {
unregisterTimeout(key);
}
timeouts.put(key, new Timeout<>(timeoutListener, key, delay, unit, scheduledExecutorService));
} | Register a timeout for the given key which shall occur in the given delay.
@param key for which to register the timeout
@param delay until the timeout
@param unit of the timeout delay |
public void unregisterTimeout(K key) {
Timeout<K> timeout = timeouts.remove(key);
if (timeout != null) {
timeout.cancel();
}
} | Unregister the timeout for the given key.
@param key for which to unregister the timeout |
protected void unregisterAllTimeouts() {
for (Timeout<K> timeout : timeouts.values()) {
timeout.cancel();
}
timeouts.clear();
} | Unregister all timeouts. |
public boolean isValid(K key, UUID ticket) {
if (timeouts.containsKey(key)) {
Timeout<K> timeout = timeouts.get(key);
return timeout.getTicket().equals(ticket);
} else {
return false;
}
} | Check whether the timeout for the given key and ticket is still valid (not yet unregistered
and not yet overwritten).
@param key for which to check the timeout
@param ticket of the timeout
@return True if the timeout ticket is still valid; otherwise false |
public static MemorySegment allocateUnpooledOffHeapMemory(int size, Object owner) {
ByteBuffer memory = ByteBuffer.allocateDirect(size);
return wrapPooledOffHeapMemory(memory, owner);
} | Allocates some unpooled off-heap memory and creates a new memory segment that
represents that memory.
@param size The size of the off-heap memory segment to allocate.
@param owner The owner to associate with the off-heap memory segment.
@return A new memory segment, backed by unpooled off-heap memory. |
public static RefCountedFile newFile(
final File file,
final OutputStream currentOut) throws IOException {
return new RefCountedFile(file, currentOut, 0L);
} | ------------------------------ Factory methods for initializing a temporary file ------------------------------ |
@Override
public void writeBlock(Buffer buffer) throws IOException {
try {
// if successfully added, the buffer will be recycled after the write operation
addRequest(new BufferWriteRequest(this, buffer));
} catch (Throwable e) {
// if not added, we need to recycle here
buffer.recycleBuffer();
ExceptionUtils.rethrowIOException(e);
}
} | Writes the given block asynchronously.
@param buffer
the buffer to be written (will be recycled when done)
@throws IOException
thrown if adding the write operation fails |
public void setNumFields(final int numFields) {
final int oldNumFields = this.numFields;
// check whether we increase or decrease the fields
if (numFields > oldNumFields) {
makeSpace(numFields);
for (int i = oldNumFields; i < numFields; i++) {
this.offsets[i] = NULL_INDICATOR_OFFSET;
}
markModified(oldNumFields);
}
else {
// decrease the number of fields
// we do not remove the values from the cache, as the objects (if they are there) will most likely
// be reused when the record is re-filled
markModified(numFields);
}
this.numFields = numFields;
} | Sets the number of fields in the record. If the new number of fields is longer than the current number of
fields, then null fields are appended. If the new number of fields is smaller than the current number of
fields, then the last fields are truncated.
@param numFields The new number of fields. |
public void makeSpace(int numFields) {
final int oldNumFields = this.numFields;
// increase the number of fields in the arrays
if (this.offsets == null) {
this.offsets = new int[numFields];
}
else if (this.offsets.length < numFields) {
int[] newOffs = new int[Math.max(numFields + 1, oldNumFields << 1)];
System.arraycopy(this.offsets, 0, newOffs, 0, oldNumFields);
this.offsets = newOffs;
}
if (this.lengths == null) {
this.lengths = new int[numFields];
}
else if (this.lengths.length < numFields) {
int[] newLens = new int[Math.max(numFields + 1, oldNumFields << 1)];
System.arraycopy(this.lengths, 0, newLens, 0, oldNumFields);
this.lengths = newLens;
}
if (this.readFields == null) {
this.readFields = new Value[numFields];
}
else if (this.readFields.length < numFields) {
Value[] newFields = new Value[Math.max(numFields + 1, oldNumFields << 1)];
System.arraycopy(this.readFields, 0, newFields, 0, oldNumFields);
this.readFields = newFields;
}
if (this.writeFields == null) {
this.writeFields = new Value[numFields];
}
else if (this.writeFields.length < numFields) {
Value[] newFields = new Value[Math.max(numFields + 1, oldNumFields << 1)];
System.arraycopy(this.writeFields, 0, newFields, 0, oldNumFields);
this.writeFields = newFields;
}
} | Reserves space for at least the given number of fields in the internal arrays.
@param numFields The number of fields to reserve space for. |
@SuppressWarnings("unchecked")
public <T extends Value> T getField(final int fieldNum, final Class<T> type) {
// range check
if (fieldNum < 0 || fieldNum >= this.numFields) {
throw new IndexOutOfBoundsException(fieldNum + " for range [0.." + (this.numFields - 1) + "]");
}
// get offset and check for null
final int offset = this.offsets[fieldNum];
if (offset == NULL_INDICATOR_OFFSET) {
return null;
}
else if (offset == MODIFIED_INDICATOR_OFFSET) {
// value that has been set is new or modified
return (T) this.writeFields[fieldNum];
}
final int limit = offset + this.lengths[fieldNum];
// get an instance, either from the instance cache or create a new one
final Value oldField = this.readFields[fieldNum];
final T field;
if (oldField != null && oldField.getClass() == type) {
field = (T) oldField;
}
else {
field = InstantiationUtil.instantiate(type, Value.class);
this.readFields[fieldNum] = field;
}
// deserialize
deserialize(field, offset, limit, fieldNum);
return field;
} | Gets the field at the given position from the record. This method checks internally, if this instance of
the record has previously returned a value for this field. If so, it reuses the object, if not, it
creates one from the supplied class.
@param <T> The type of the field.
@param fieldNum The logical position of the field.
@param type The type of the field as a class. This class is used to instantiate a value object, if none had
previously been instantiated.
@return The field at the given position, or null, if the field was null.
@throws IndexOutOfBoundsException Thrown, if the field number is negative or larger or equal to the number of
fields in this record. |
@SuppressWarnings("unchecked")
public <T extends Value> T getField(int fieldNum, T target) {
// range check
if (fieldNum < 0 || fieldNum >= this.numFields) {
throw new IndexOutOfBoundsException();
}
if (target == null) {
throw new NullPointerException("The target object may not be null");
}
// get offset and check for null
final int offset = this.offsets[fieldNum];
if (offset == NULL_INDICATOR_OFFSET) {
return null;
}
else if (offset == MODIFIED_INDICATOR_OFFSET) {
// value that has been set is new or modified
// bring the binary in sync so that the deserialization gives the correct result
return (T) this.writeFields[fieldNum];
}
final int limit = offset + this.lengths[fieldNum];
deserialize(target, offset, limit, fieldNum);
return target;
} | Gets the field at the given position. The method tries to deserialize the fields into the given target value.
If the fields has been changed since the last (de)serialization, or is null, them the target value is left
unchanged and the changed value (or null) is returned.
<p>
In all cases, the returned value contains the correct data (or is correctly null).
@param fieldNum The position of the field.
@param target The value to deserialize the field into.
@return The value with the contents of the requested field, or null, if the field is null. |
public boolean getFieldInto(int fieldNum, Value target) {
// range check
if (fieldNum < 0 || fieldNum >= this.numFields) {
throw new IndexOutOfBoundsException();
}
// get offset and check for null
int offset = this.offsets[fieldNum];
if (offset == NULL_INDICATOR_OFFSET) {
return false;
}
else if (offset == MODIFIED_INDICATOR_OFFSET) {
// value that has been set is new or modified
// bring the binary in sync so that the deserialization gives the correct result
updateBinaryRepresenation();
offset = this.offsets[fieldNum];
}
final int limit = offset + this.lengths[fieldNum];
deserialize(target, offset, limit, fieldNum);
return true;
} | Gets the field at the given position. If the field at that position is null, then this method leaves
the target field unchanged and returns false.
@param fieldNum The position of the field.
@param target The value to deserialize the field into.
@return True, if the field was deserialized properly, false, if the field was null. |
public boolean getFieldsInto(int[] positions, Value[] targets) {
for (int i = 0; i < positions.length; i++) {
if (!getFieldInto(positions[i], targets[i])) {
return false;
}
}
return true;
} | Gets the fields at the given positions into an array.
If at any position a field is null, then this method returns false.
All fields that have been successfully read until the failing read are correctly contained in the record.
All other fields are not set.
@param positions The positions of the fields to get.
@param targets The values into which the content of the fields is put.
@return True if all fields were successfully read, false if some read failed. |
public void getFieldsIntoCheckingNull(int[] positions, Value[] targets) {
for (int i = 0; i < positions.length; i++) {
if (!getFieldInto(positions[i], targets[i])) {
throw new NullKeyFieldException(i);
}
}
} | Gets the fields at the given positions into an array.
If at any position a field is null, then this method throws a @link NullKeyFieldException.
All fields that have been successfully read until the failing read are correctly contained in the record.
All other fields are not set.
@param positions The positions of the fields to get.
@param targets The values into which the content of the fields is put.
@throws NullKeyFieldException in case of a failing field read. |
public void setField(int fieldNum, Value value) {
// range check
if (fieldNum < 0) {
throw new IndexOutOfBoundsException();
}
// if the field number is beyond the size, the tuple is expanded
if (fieldNum >= this.numFields) {
setNumFields(fieldNum + 1);
}
internallySetField(fieldNum, value);
} | Sets the field at the given position to the given value. If the field position is larger or equal than
the current number of fields in the record, than the record is expanded to host as many columns.
<p>
The value is kept as a reference in the record until the binary representation is synchronized. Until that
point, all modifications to the value's object will change the value inside the record.
<p>
The binary representation is synchronized the latest when the record is emitted. It may be triggered
manually at an earlier point, but it is generally not necessary and advisable. Because the synchronization
triggers the serialization on all modified values, it may be an expensive operation.
@param fieldNum The position of the field, starting at zero.
@param value The new value. |
public void removeField(int fieldNum)
{
// range check
if (fieldNum < 0 || fieldNum >= this.numFields) {
throw new IndexOutOfBoundsException();
}
int lastIndex = this.numFields - 1;
if (fieldNum < lastIndex) {
int len = lastIndex - fieldNum;
System.arraycopy(this.offsets, fieldNum + 1, this.offsets, fieldNum, len);
System.arraycopy(this.lengths, fieldNum + 1, this.lengths, fieldNum, len);
System.arraycopy(this.readFields, fieldNum + 1, this.readFields, fieldNum, len);
System.arraycopy(this.writeFields, fieldNum + 1, this.writeFields, fieldNum, len);
markModified(fieldNum);
}
this.offsets[lastIndex] = NULL_INDICATOR_OFFSET;
this.lengths[lastIndex] = 0;
this.writeFields[lastIndex] = null;
setNumFields(lastIndex);
} | Removes the field at the given position.
<p>
This method should be used carefully. Be aware that as the field is actually removed from the record, the
total number of fields is modified, and all fields to the right of the field removed shift one position to
the left.
@param fieldNum The position of the field to be removed, starting at zero.
@throws IndexOutOfBoundsException Thrown, when the position is not between 0 (inclusive) and the
number of fields (exclusive). |
public void setNull(long mask) {
for (int i = 0; i < this.numFields; i++, mask >>>= 1) {
if ((mask & 0x1) != 0) {
internallySetField(i, null);
}
}
} | Sets the fields to <code>null</code> using the given bit mask.
The bits correspond to the individual columns: <code>(1 == nullify, 0 == keep)</code>.
@param mask Bit mask, where the i-th least significant bit represents the i-th field in the record. |
public void setNull(long[] mask) {
for (int maskPos = 0, i = 0; i < this.numFields;) {
long currMask = mask[maskPos];
for (int k = 64; i < this.numFields && k > 0; --k, i++, currMask >>>= 1) {
if ((currMask & 0x1) != 0) {
internallySetField(i, null);
}
}
}
} | Sets the fields to <code>null</code> using the given bit mask.
The bits correspond to the individual columns: <code>(1 == nullify, 0 == keep)</code>.
@param mask Bit mask, where the i-th least significant bit in the n-th bit mask represents the
<code>(n*64) + i</code>-th field in the record. |
public void unionFields(Record other) {
final int minFields = Math.min(this.numFields, other.numFields);
final int maxFields = Math.max(this.numFields, other.numFields);
final int[] offsets = this.offsets.length >= maxFields ? this.offsets : new int[maxFields];
final int[] lengths = this.lengths.length >= maxFields ? this.lengths : new int[maxFields];
if (!(this.isModified() || other.isModified())) {
// handle the special (but common) case where both records have a valid binary representation differently
// allocate space for the switchBuffer first
final int estimatedLength = this.binaryLen + other.binaryLen;
this.serializer.memory = (this.switchBuffer != null && this.switchBuffer.length >= estimatedLength) ?
this.switchBuffer : new byte[estimatedLength];
this.serializer.position = 0;
try {
// common loop for both records
for (int i = 0; i < minFields; i++) {
final int thisOff = this.offsets[i];
if (thisOff == NULL_INDICATOR_OFFSET) {
final int otherOff = other.offsets[i];
if (otherOff == NULL_INDICATOR_OFFSET) {
offsets[i] = NULL_INDICATOR_OFFSET;
} else {
// take field from other record
offsets[i] = this.serializer.position;
this.serializer.write(other.binaryData, otherOff, other.lengths[i]);
lengths[i] = other.lengths[i];
}
} else {
// copy field from this one
offsets[i] = this.serializer.position;
this.serializer.write(this.binaryData, thisOff, this.lengths[i]);
lengths[i] = this.lengths[i];
}
}
// add the trailing fields from one record
if (minFields != maxFields) {
final Record sourceForRemainder = this.numFields > minFields ? this : other;
int begin = -1;
int end = -1;
int offsetDelta = 0;
// go through the offsets, find the non-null fields to account for the remaining data
for (int k = minFields; k < maxFields; k++) {
final int off = sourceForRemainder.offsets[k];
if (off == NULL_INDICATOR_OFFSET) {
offsets[k] = NULL_INDICATOR_OFFSET;
} else {
end = sourceForRemainder.offsets[k]+sourceForRemainder.lengths[k];
if (begin == -1) {
// first non null column in the remainder
begin = sourceForRemainder.offsets[k];
offsetDelta = this.serializer.position - begin;
}
offsets[k] = sourceForRemainder.offsets[k] + offsetDelta;
}
}
// copy the remaining fields directly as binary
if (begin != -1) {
this.serializer.write(sourceForRemainder.binaryData, begin,
end - begin);
}
// the lengths can be copied directly
if (lengths != sourceForRemainder.lengths) {
System.arraycopy(sourceForRemainder.lengths, minFields, lengths, minFields, maxFields - minFields);
}
}
} catch (Exception ioex) {
throw new RuntimeException("Error creating field union of record data" +
ioex.getMessage() == null ? "." : ": " + ioex.getMessage(), ioex);
}
}
else {
// the general case, where at least one of the two records has a binary representation that is not in sync.
final int estimatedLength = (this.binaryLen > 0 ? this.binaryLen : this.numFields * DEFAULT_FIELD_LEN_ESTIMATE) +
(other.binaryLen > 0 ? other.binaryLen : other.numFields * DEFAULT_FIELD_LEN_ESTIMATE);
this.serializer.memory = (this.switchBuffer != null && this.switchBuffer.length >= estimatedLength) ?
this.switchBuffer : new byte[estimatedLength];
this.serializer.position = 0;
try {
// common loop for both records
for (int i = 0; i < minFields; i++) {
final int thisOff = this.offsets[i];
if (thisOff == NULL_INDICATOR_OFFSET) {
final int otherOff = other.offsets[i];
if (otherOff == NULL_INDICATOR_OFFSET) {
offsets[i] = NULL_INDICATOR_OFFSET;
} else if (otherOff == MODIFIED_INDICATOR_OFFSET) {
// serialize modified field from other record
offsets[i] = this.serializer.position;
other.writeFields[i].write(this.serializer);
lengths[i] = this.serializer.position - offsets[i];
} else {
// take field from other record binary
offsets[i] = this.serializer.position;
this.serializer.write(other.binaryData, otherOff, other.lengths[i]);
lengths[i] = other.lengths[i];
}
} else if (thisOff == MODIFIED_INDICATOR_OFFSET) {
// serialize modified field from this record
offsets[i] = this.serializer.position;
this.writeFields[i].write(this.serializer);
lengths[i] = this.serializer.position - offsets[i];
} else {
// copy field from this one
offsets[i] = this.serializer.position;
this.serializer.write(this.binaryData, thisOff, this.lengths[i]);
lengths[i] = this.lengths[i];
}
}
// add the trailing fields from one record
if (minFields != maxFields) {
final Record sourceForRemainder = this.numFields > minFields ? this : other;
// go through the offsets, find the non-null fields
for (int k = minFields; k < maxFields; k++) {
final int off = sourceForRemainder.offsets[k];
if (off == NULL_INDICATOR_OFFSET) {
offsets[k] = NULL_INDICATOR_OFFSET;
} else if (off == MODIFIED_INDICATOR_OFFSET) {
// serialize modified field from the source record
offsets[k] = this.serializer.position;
sourceForRemainder.writeFields[k].write(this.serializer);
lengths[k] = this.serializer.position - offsets[k];
} else {
// copy field from the source record binary
offsets[k] = this.serializer.position;
final int len = sourceForRemainder.lengths[k];
this.serializer.write(sourceForRemainder.binaryData, off, len);
lengths[k] = len;
}
}
}
} catch (Exception ioex) {
throw new RuntimeException("Error creating field union of record data" +
ioex.getMessage() == null ? "." : ": " + ioex.getMessage(), ioex);
}
}
serializeHeader(this.serializer, offsets, maxFields);
// set the fields
this.switchBuffer = this.binaryData;
this.binaryData = serializer.memory;
this.binaryLen = serializer.position;
this.numFields = maxFields;
this.offsets = offsets;
this.lengths = lengths;
this.firstModifiedPos = Integer.MAX_VALUE;
// make sure that the object arrays reflect the size as well
if (this.readFields == null || this.readFields.length < maxFields) {
final Value[] na = new Value[maxFields];
System.arraycopy(this.readFields, 0, na, 0, this.readFields.length);
this.readFields = na;
}
this.writeFields = (this.writeFields == null || this.writeFields.length < maxFields) ?
new Value[maxFields] : this.writeFields;
} | Unions the other record's fields with this records fields. After the method invocation with record
<code>B</code> as the parameter, this record <code>A</code> will contain at field <code>i</code>:
<ul>
<li>Field <code>i</code> from record <code>A</code>, if that field is within record <code>A</code>'s number
of fields and is not <i>null</i>.</li>
<li>Field <code>i</code> from record <code>B</code>, if that field is within record <code>B</code>'s number
of fields.</li>
</ul>
It is not necessary that both records have the same number of fields. This record will have the number of
fields of the larger of the two records. Naturally, if both <code>A</code> and <code>B</code> have field
<code>i</code> set to <i>null</i>, this record will have <i>null</i> at that position.
@param other The records whose fields to union with this record's fields. |
public void copyFrom(final Record source, final int[] sourcePositions, final int[] targetPositions) {
final int[] sourceOffsets = source.offsets;
final int[] sourceLengths = source.lengths;
final byte[] sourceBuffer = source.binaryData;
final Value[] sourceFields = source.writeFields;
boolean anyFieldIsBinary = false;
int maxFieldNum = 0;
for (int i = 0; i < sourcePositions.length; i++) {
final int sourceFieldNum = sourcePositions[i];
final int sourceOffset = sourceOffsets[sourceFieldNum];
final int targetFieldNum = targetPositions[i];
maxFieldNum = Math.max(targetFieldNum, maxFieldNum);
if (sourceOffset == NULL_INDICATOR_OFFSET) {
// set null on existing field (new fields are null by default)
if (targetFieldNum < numFields) {
internallySetField(targetFieldNum, null);
}
} else if (sourceOffset != MODIFIED_INDICATOR_OFFSET) {
anyFieldIsBinary = true;
}
}
if (numFields < maxFieldNum + 1) {
setNumFields(maxFieldNum + 1);
}
final int[] targetLengths = this.lengths;
final int[] targetOffsets = this.offsets;
// reserve space in binaryData for the binary source fields
if (anyFieldIsBinary) {
for (int i = 0; i < sourcePositions.length; i++) {
final int sourceFieldNum = sourcePositions[i];
final int sourceOffset = sourceOffsets[sourceFieldNum];
if (sourceOffset != MODIFIED_INDICATOR_OFFSET && sourceOffset != NULL_INDICATOR_OFFSET) {
final int targetFieldNum = targetPositions[i];
targetLengths[targetFieldNum] = sourceLengths[sourceFieldNum];
internallySetField(targetFieldNum, RESERVE_SPACE);
}
}
updateBinaryRepresenation();
}
final byte[] targetBuffer = this.binaryData;
for (int i = 0; i < sourcePositions.length; i++) {
final int sourceFieldNum = sourcePositions[i];
final int sourceOffset = sourceOffsets[sourceFieldNum];
final int targetFieldNum = targetPositions[i];
if (sourceOffset == MODIFIED_INDICATOR_OFFSET) {
internallySetField(targetFieldNum, sourceFields[sourceFieldNum]);
} else if (sourceOffset != NULL_INDICATOR_OFFSET) {
// bin-copy
final int targetOffset = targetOffsets[targetFieldNum];
final int length = targetLengths[targetFieldNum];
System.arraycopy(sourceBuffer, sourceOffset, targetBuffer, targetOffset, length);
}
}
} | Bin-copies fields from a source record to this record. The following caveats apply:
If the source field is in a modified state, no binary representation will exist yet.
In that case, this method is equivalent to {@code setField(..., source.getField(..., <class>))}.
In particular, if setValue is called on the source field Value instance, that change
will propagate to this record.
If the source field has already been serialized, then the binary representation
will be copied. Further modifications to the source field will not be observable
via this record, but attempting to read the field from this record will cause it
to be deserialized.
Finally, bin-copying a source field requires calling updateBinaryRepresentation
on this instance in order to reserve space in the binaryData array. If none
of the source fields are actually bin-copied, then updateBinaryRepresentation
won't be called.
@param source
@param sourcePositions
@param targetPositions |
public final boolean equalsFields(int[] positions, Value[] searchValues, Value[] deserializationHolders) {
for (int i = 0; i < positions.length; i++) {
final Value v = getField(positions[i], deserializationHolders[i]);
if (v == null || (!v.equals(searchValues[i]))) {
return false;
}
}
return true;
} | Checks the values of this record and a given list of values at specified positions for equality.
The values of this record are deserialized and compared against the corresponding search value.
The position specify which values are compared.
The method returns true if the values on all positions are equal and false otherwise.
@param positions The positions of the values to check for equality.
@param searchValues The values against which the values of this record are compared.
@param deserializationHolders An array to hold the deserialized values of this record.
@return True if all the values on all positions are equal, false otherwise. |
public void updateBinaryRepresenation() {
// check whether the binary state is in sync
final int firstModified = this.firstModifiedPos;
if (firstModified == Integer.MAX_VALUE) {
return;
}
final InternalDeSerializer serializer = this.serializer;
final int[] offsets = this.offsets;
final int numFields = this.numFields;
serializer.memory = this.switchBuffer != null ? this.switchBuffer :
(this.binaryLen > 0 ? new byte[this.binaryLen] : new byte[numFields * DEFAULT_FIELD_LEN_ESTIMATE + 1]);
serializer.position = 0;
if (numFields > 0) {
int offset = 0;
// search backwards to find the latest preceding non-null field
if (firstModified > 0) {
for (int i = firstModified - 1; i >= 0; i--) {
if (this.offsets[i] != NULL_INDICATOR_OFFSET) {
offset = this.offsets[i] + this.lengths[i];
break;
}
}
}
// we assume that changed and unchanged fields are interleaved and serialize into another array
try {
if (offset > 0) {
// copy the first unchanged portion as one
serializer.write(this.binaryData, 0, offset);
}
// copy field by field
for (int i = firstModified; i < numFields; i++) {
final int co = offsets[i];
/// skip null fields
if (co == NULL_INDICATOR_OFFSET) {
continue;
}
offsets[i] = offset;
if (co == MODIFIED_INDICATOR_OFFSET) {
final Value writeField = this.writeFields[i];
if (writeField == RESERVE_SPACE) {
// RESERVE_SPACE is a placeholder indicating lengths[i] bytes should be reserved
final int length = this.lengths[i];
if (serializer.position >= serializer.memory.length - length - 1) {
serializer.resize(length);
}
serializer.position += length;
} else {
// serialize modified fields
this.writeFields[i].write(serializer);
}
} else {
// bin-copy unmodified fields
serializer.write(this.binaryData, co, this.lengths[i]);
}
this.lengths[i] = serializer.position - offset;
offset = serializer.position;
}
}
catch (Exception e) {
throw new RuntimeException("Error in data type serialization: " + e.getMessage(), e);
}
}
serializeHeader(serializer, offsets, numFields);
// set the fields
this.switchBuffer = this.binaryData;
this.binaryData = serializer.memory;
this.binaryLen = serializer.position;
this.firstModifiedPos = Integer.MAX_VALUE;
} | Updates the binary representation of the data, such that it reflects the state of the currently
stored fields. If the binary representation is already up to date, nothing happens. Otherwise,
this function triggers the modified fields to serialize themselves into the records buffer and
afterwards updates the offset table. |
@Override
public void write(DataOutputView out) throws IOException {
// make sure everything is in a valid binary representation
updateBinaryRepresenation();
// write the length first, variably encoded, then the contents of the binary array
writeVarLengthInt(out, this.binaryLen);
out.write(this.binaryData, 0, this.binaryLen);
} | -------------------------------------------------------------------------------------------- |
public long serialize(DataOutputView target) throws IOException {
updateBinaryRepresenation();
long bytesForLen = 1;
int len = this.binaryLen;
while (len >= MAX_BIT) {
target.write(len | MAX_BIT);
len >>= 7;
bytesForLen++;
}
target.write(len);
target.write(this.binaryData, 0, this.binaryLen);
return bytesForLen + this.binaryLen;
} | Writes this record to the given output view. This method is similar to {@link org.apache.flink.core.io.IOReadableWritable#write(org.apache.flink.core.memory.DataOutputView)}, but
it returns the number of bytes written.
@param target The view to write the record to.
@return The number of bytes written.
@throws IOException Thrown, if an error occurred in the view during writing. |
private static final void writeVarLengthInt(DataOutput out, int value) throws IOException {
while (value >= MAX_BIT) {
out.write(value | MAX_BIT);
value >>= 7;
}
out.write(value);
} | -------------------------------------------------------------------------------------------- |
Subsets and Splits
No community queries yet
The top public SQL queries from the community will appear here once available.