Java Code Examples for org.apache.flink.util.Preconditions#checkArgument()

The following examples show how to use org.apache.flink.util.Preconditions#checkArgument() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: GPUDriver.java    From flink with Apache License 2.0 6 votes vote down vote up
@Override
public Set<GPUInfo> retrieveResourceInfo(long gpuAmount) throws Exception {
	Preconditions.checkArgument(gpuAmount > 0, "The gpuAmount should be positive when retrieving the GPU resource information.");

	final Set<GPUInfo> gpuResources = new HashSet<>();
	String output = executeDiscoveryScript(discoveryScriptFile, gpuAmount, args);
	if (!output.isEmpty()) {
		String[] indexes = output.split(",");
		for (String index : indexes) {
			if (!StringUtils.isNullOrWhitespaceOnly(index)) {
				gpuResources.add(new GPUInfo(index.trim()));
			}
		}
	}
	LOG.info("Discover GPU resources: {}.", gpuResources);
	return Collections.unmodifiableSet(gpuResources);
}
 
Example 2
Source File: GraphGeneratorUtils.java    From Flink-CEPplus with Apache License 2.0 6 votes vote down vote up
/**
 * Generates {@link Vertex Vertices} with sequential, numerical labels.
 *
 * @param env the Flink execution environment.
 * @param parallelism operator parallelism
 * @param vertexCount number of sequential vertex labels
 * @return {@link DataSet} of sequentially labeled {@link Vertex vertices}
 */
public static DataSet<Vertex<LongValue, NullValue>> vertexSequence(ExecutionEnvironment env, int parallelism, long vertexCount) {
	Preconditions.checkArgument(vertexCount >= 0, "Vertex count must be non-negative");

	if (vertexCount == 0) {
		return env
			.fromCollection(Collections.emptyList(), TypeInformation.of(new TypeHint<Vertex<LongValue, NullValue>>(){}))
				.setParallelism(parallelism)
				.name("Empty vertex set");
	} else {
		LongValueSequenceIterator iterator = new LongValueSequenceIterator(0, vertexCount - 1);

		DataSource<LongValue> vertexLabels = env
			.fromParallelCollection(iterator, LongValue.class)
				.setParallelism(parallelism)
				.name("Vertex indices");

		return vertexLabels
			.map(new CreateVertex())
				.setParallelism(parallelism)
				.name("Vertex sequence");
	}
}
 
Example 3
Source File: DelimitedInputFormat.java    From Flink-CEPplus with Apache License 2.0 5 votes vote down vote up
@PublicEvolving
@Override
public void reopen(FileInputSplit split, Long state) throws IOException {
	Preconditions.checkNotNull(split, "reopen() cannot be called on a null split.");
	Preconditions.checkNotNull(state, "reopen() cannot be called with a null initial state.");
	Preconditions.checkArgument(state == -1 || state >= split.getStart(),
		" Illegal offset "+ state +", smaller than the splits start=" + split.getStart());

	try {
		this.open(split);
	} finally {
		this.offset = state;
	}

	if (state > this.splitStart + split.getLength()) {
		this.end = true;
	} else if (state > split.getStart()) {
		initBuffers();

		this.stream.seek(this.offset);
		if (split.getLength() == -1) {
			// this is the case for unsplittable files
			fillBuffer(0);
		} else {
			this.splitLength = this.splitStart + split.getLength() - this.offset;
			if (splitLength <= 0) {
				this.end = true;
			}
		}
	}
}
 
Example 4
Source File: Operator.java    From flink with Apache License 2.0 5 votes vote down vote up
/**
 * 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 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;
}
 
Example 5
Source File: SubsequenceInputTypeStrategy.java    From flink with Apache License 2.0 5 votes vote down vote up
/**
 * Defines a common {@link InputTypeStrategy} for the next arguments. Given
 * input strategy must expect a varying number of arguments. That means that the
 * maximum number of arguments must not be defined.
 */
public InputTypeStrategy finishWithVarying(InputTypeStrategy inputTypeStrategy) {
	Preconditions.checkArgument(inputTypeStrategy.getArgumentCount() instanceof ConstantArgumentCount);
	ArgumentCount strategyArgumentCount = inputTypeStrategy.getArgumentCount();
	strategyArgumentCount.getMaxCount().ifPresent(c -> {
		throw new IllegalArgumentException("The maximum number of arguments must not be defined.");
	});
	argumentsSplits.add(new ArgumentsSplit(currentPos, null, inputTypeStrategy));
	int minCount = currentPos + strategyArgumentCount.getMinCount().orElse(0);
	return new SubsequenceInputTypeStrategy(argumentsSplits, ConstantArgumentCount.from(minCount));
}
 
Example 6
Source File: ElasticsearchSink.java    From flink with Apache License 2.0 5 votes vote down vote up
/**
 * Sets the maximum number of retries for a backoff attempt when flushing bulk requests.
 *
 * @param maxRetries the maximum number of retries for a backoff attempt when flushing bulk requests
 */
public void setBulkFlushBackoffRetries(int maxRetries) {
	Preconditions.checkArgument(
		maxRetries > 0,
		"Max number of backoff attempts must be larger than 0.");

	this.bulkRequestsConfig.put(CONFIG_KEY_BULK_FLUSH_BACKOFF_RETRIES, String.valueOf(maxRetries));
}
 
Example 7
Source File: ListAggWsWithRetractAggFunctionTest.java    From flink with Apache License 2.0 5 votes vote down vote up
@Override
protected Tuple2<List<StringData>, List<StringData>> splitValues(List<StringData> values) {
	Preconditions.checkArgument(values.size() % 2 == 0,
			"number of values must be an integer multiple of 2.");
	int index = values.size() / 2;
	if (index % 2 != 0) {
		index -= 1;
	}
	return super.splitValues(values, index);
}
 
Example 8
Source File: HiveDB.java    From Alink with Apache License 2.0 5 votes vote down vote up
private static Map<String, String> getStaticPartitionSpec(String partitionSpec) {
    Map<String, String> spec = new HashMap<>();
    if (!StringUtils.isNullOrWhitespaceOnly(partitionSpec)) {
        String[] partitions = partitionSpec.split("/");
        for (int i = 0; i < partitions.length; i++) {
            String p = partitions[i];
            int pos = p.indexOf('=');
            Preconditions.checkArgument(pos > 0);
            String col = p.substring(0, pos);
            String val = p.substring(pos + 1);
            spec.put(col, val);
        }
    }
    return spec;
}
 
Example 9
Source File: DefaultConfigurableOptionsFactory.java    From flink with Apache License 2.0 5 votes vote down vote up
/**
 * Sets the configuration with (key, value) if the key is predefined, otherwise throws IllegalArgumentException.
 *
 * @param key The configuration key, if key is not predefined, throws IllegalArgumentException out.
 * @param value The configuration value.
 */
private void setInternal(String key, String value) {
	Preconditions.checkArgument(value != null && !value.isEmpty(),
		"The configuration value must not be empty.");

	configuredOptions.put(key, value);
}
 
Example 10
Source File: OperatorBackendSerializationProxy.java    From flink with Apache License 2.0 5 votes vote down vote up
public OperatorBackendSerializationProxy(
		List<StateMetaInfoSnapshot> operatorStateMetaInfoSnapshots,
		List<StateMetaInfoSnapshot> broadcastStateMetaInfoSnapshots) {

	this.operatorStateMetaInfoSnapshots = Preconditions.checkNotNull(operatorStateMetaInfoSnapshots);
	this.broadcastStateMetaInfoSnapshots = Preconditions.checkNotNull(broadcastStateMetaInfoSnapshots);
	Preconditions.checkArgument(
			operatorStateMetaInfoSnapshots.size() <= Short.MAX_VALUE &&
					broadcastStateMetaInfoSnapshots.size() <= Short.MAX_VALUE
	);
}
 
Example 11
Source File: StreamTransformation.java    From Flink-CEPplus with Apache License 2.0 5 votes vote down vote up
/**
 * Sets the parallelism of this {@code StreamTransformation}.
 *
 * @param parallelism The new parallelism to set on this {@code StreamTransformation}.
 */
public void 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;
}
 
Example 12
Source File: PartitionOperator.java    From flink with Apache License 2.0 5 votes vote down vote up
/**
 * Sets the order of keys for range partitioning.
 * NOTE: Only valid for {@link PartitionMethod#RANGE}.
 *
 * @param orders array of orders for each specified partition key
 * @return The partitioneOperator with properly set orders for given keys
    */
@PublicEvolving
public PartitionOperator<T> withOrders(Order... orders) {
	Preconditions.checkState(pMethod == PartitionMethod.RANGE, "Orders cannot be applied for %s partition " +
			"method", pMethod);
	Preconditions.checkArgument(pKeys.getOriginalKeyFieldTypes().length == orders.length, "The number of key " +
			"fields and orders should be the same.");
	this.orders = orders;

	return this;
}
 
Example 13
Source File: ArchivedExecutionBuilder.java    From flink with Apache License 2.0 4 votes vote down vote up
public ArchivedExecutionBuilder setStateTimestamps(long[] stateTimestamps) {
	Preconditions.checkArgument(stateTimestamps.length == ExecutionState.values().length);
	this.stateTimestamps = stateTimestamps;
	return this;
}
 
Example 14
Source File: TtlStateFactory.java    From Flink-CEPplus with Apache License 2.0 4 votes vote down vote up
@SuppressWarnings("unchecked")
@Override
public TtlValue<T> createInstance(@Nonnull Object ... values) {
	Preconditions.checkArgument(values.length == 2);
	return new TtlValue<>((T) values[1], (long) values[0]);
}
 
Example 15
Source File: MaximumDegree.java    From flink with Apache License 2.0 4 votes vote down vote up
/**
 * Filter out vertices with degree greater than the given maximum.
 *
 * @param maximumDegree maximum degree
 */
public MaximumDegree(long maximumDegree) {
	Preconditions.checkArgument(maximumDegree > 0, "Maximum degree must be greater than zero");

	this.maximumDegree = maximumDegree;
}
 
Example 16
Source File: DefaultConfigurableOptionsFactory.java    From flink with Apache License 2.0 4 votes vote down vote up
public DefaultConfigurableOptionsFactory setMaxBackgroundThreads(int totalThreadCount) {
	Preconditions.checkArgument(totalThreadCount > 0);
	configuredOptions.put(MAX_BACKGROUND_THREADS.key(), String.valueOf(totalThreadCount));
	return this;
}
 
Example 17
Source File: SingletonEdgeGraph.java    From Flink-CEPplus with Apache License 2.0 3 votes vote down vote up
/**
 * An undirected {@link Graph} containing one or more isolated two-paths.
 * The in- and out-degree of every vertex is 1. For {@code n} vertices
 * there are {@code n/2} components.
 *
 * @param env the Flink execution environment
 * @param vertexPairCount number of pairs of vertices
 */
public SingletonEdgeGraph(ExecutionEnvironment env, long vertexPairCount) {
	Preconditions.checkArgument(vertexPairCount >= MINIMUM_VERTEX_PAIR_COUNT,
		"Vertex pair count must be at least " + MINIMUM_VERTEX_PAIR_COUNT);

	this.env = env;
	this.vertexPairCount = vertexPairCount;
}
 
Example 18
Source File: SingletonEdgeGraph.java    From flink with Apache License 2.0 3 votes vote down vote up
/**
 * An undirected {@link Graph} containing one or more isolated two-paths.
 * The in- and out-degree of every vertex is 1. For {@code n} vertices
 * there are {@code n/2} components.
 *
 * @param env the Flink execution environment
 * @param vertexPairCount number of pairs of vertices
 */
public SingletonEdgeGraph(ExecutionEnvironment env, long vertexPairCount) {
	Preconditions.checkArgument(vertexPairCount >= MINIMUM_VERTEX_PAIR_COUNT,
		"Vertex pair count must be at least " + MINIMUM_VERTEX_PAIR_COUNT);

	this.env = env;
	this.vertexPairCount = vertexPairCount;
}
 
Example 19
Source File: StateAssignmentOperation.java    From flink with Apache License 2.0 3 votes vote down vote up
/**
 * Groups the available set of key groups into key group partitions. A key group partition is
 * the set of key groups which is assigned to the same task. Each set of the returned list
 * constitutes a key group partition.
 *
 * <p><b>IMPORTANT</b>: The assignment of key groups to partitions has to be in sync with the
 * KeyGroupStreamPartitioner.
 *
 * @param numberKeyGroups Number of available key groups (indexed from 0 to numberKeyGroups - 1)
 * @param parallelism     Parallelism to generate the key group partitioning for
 * @return List of key group partitions
 */
public static List<KeyGroupRange> createKeyGroupPartitions(int numberKeyGroups, int parallelism) {
	Preconditions.checkArgument(numberKeyGroups >= parallelism);
	List<KeyGroupRange> result = new ArrayList<>(parallelism);

	for (int i = 0; i < parallelism; ++i) {
		result.add(KeyGroupRangeAssignment.computeKeyGroupRangeForOperatorIndex(numberKeyGroups, parallelism, i));
	}
	return result;
}
 
Example 20
Source File: StateAssignmentOperation.java    From Flink-CEPplus with Apache License 2.0 3 votes vote down vote up
/**
 * Groups the available set of key groups into key group partitions. A key group partition is
 * the set of key groups which is assigned to the same task. Each set of the returned list
 * constitutes a key group partition.
 * <p>
 * <b>IMPORTANT</b>: The assignment of key groups to partitions has to be in sync with the
 * KeyGroupStreamPartitioner.
 *
 * @param numberKeyGroups Number of available key groups (indexed from 0 to numberKeyGroups - 1)
 * @param parallelism     Parallelism to generate the key group partitioning for
 * @return List of key group partitions
 */
public static List<KeyGroupRange> createKeyGroupPartitions(int numberKeyGroups, int parallelism) {
	Preconditions.checkArgument(numberKeyGroups >= parallelism);
	List<KeyGroupRange> result = new ArrayList<>(parallelism);

	for (int i = 0; i < parallelism; ++i) {
		result.add(KeyGroupRangeAssignment.computeKeyGroupRangeForOperatorIndex(numberKeyGroups, parallelism, i));
	}
	return result;
}