Search in sources :

Example 11 with ComputerException

use of com.baidu.hugegraph.computer.core.common.exception.ComputerException in project hugegraph-computer by hugegraph.

the class EtcdClient method get.

/**
 * Returns the value to which the specified key is mapped. If no
 * key exists, wait at most timeout milliseconds. Or throw
 * ComputerException if timeout
 * @param key the key whose associated value is to be returned.
 * @param timeout the max time in milliseconds to wait.
 * @return the specified value in byte array to which the specified key is
 * mapped.
 */
public byte[] get(String key, long timeout, long logInterval) {
    E.checkArgumentNotNull(key, "The key can't be null");
    E.checkArgument(timeout > 0L, "The timeout must be > 0, but got: %s", timeout);
    E.checkArgument(logInterval > 0L, "The logInterval must be > 0, but got: %s", logInterval);
    ByteSequence keySeq = ByteSequence.from(key, ENCODING);
    try {
        GetResponse response = this.kv.get(keySeq).get();
        if (response.getCount() > 0) {
            List<KeyValue> kvs = response.getKvs();
            return kvs.get(0).getValue().getBytes();
        } else {
            long revision = response.getHeader().getRevision();
            return this.waitAndGetFromPutEvent(keySeq, revision, timeout, logInterval);
        }
    } catch (InterruptedException e) {
        throw new ComputerException("Interrupted while getting with key='%s'", e, key);
    } catch (ExecutionException e) {
        throw new ComputerException("Error while getting with key='%s'", e, key);
    }
}
Also used : KeyValue(io.etcd.jetcd.KeyValue) ExecutionException(java.util.concurrent.ExecutionException) GetResponse(io.etcd.jetcd.kv.GetResponse) ByteSequence(io.etcd.jetcd.ByteSequence) ComputerException(com.baidu.hugegraph.computer.core.common.exception.ComputerException)

Example 12 with ComputerException

use of com.baidu.hugegraph.computer.core.common.exception.ComputerException in project hugegraph-computer by hugegraph.

the class EtcdClient method getWithPrefix.

/**
 * Get the expected count of values of keys with the specified prefix.
 * Throws ComputerException if there are no enough object.
 */
public List<byte[]> getWithPrefix(String prefix, int count) {
    E.checkArgumentNotNull(prefix, "The prefix can't be null");
    E.checkArgument(count >= 0, "The count must be >= 0, but got: %s", count);
    try {
        ByteSequence prefixSeq = ByteSequence.from(prefix, ENCODING);
        GetOption getOption = GetOption.newBuilder().withPrefix(prefixSeq).withLimit(count).withSortOrder(SortOrder.ASCEND).build();
        GetResponse response = this.kv.get(prefixSeq, getOption).get();
        if (response.getCount() == count) {
            return getResponseValues(response);
        } else {
            throw new ComputerException("Expect %s elements, only find %s elements with " + "prefix='%s'", count, response.getCount(), prefix);
        }
    } catch (InterruptedException e) {
        throw new ComputerException("Interrupted while getting with prefix='%s', count=%s", e, prefix, count);
    } catch (ExecutionException e) {
        throw new ComputerException("Error while getting with prefix='%s', count=%s", e, prefix, count);
    }
}
Also used : GetOption(io.etcd.jetcd.options.GetOption) ExecutionException(java.util.concurrent.ExecutionException) GetResponse(io.etcd.jetcd.kv.GetResponse) ByteSequence(io.etcd.jetcd.ByteSequence) ComputerException(com.baidu.hugegraph.computer.core.common.exception.ComputerException)

Example 13 with ComputerException

use of com.baidu.hugegraph.computer.core.common.exception.ComputerException in project hugegraph-computer by hugegraph.

the class EtcdClient method deleteWithPrefix.

/**
 * @return the number of keys deleted
 */
public long deleteWithPrefix(String prefix) {
    E.checkArgumentNotNull(prefix, "The prefix can't be null");
    ByteSequence prefixSeq = ByteSequence.from(prefix, ENCODING);
    DeleteOption deleteOption = DeleteOption.newBuilder().withPrefix(prefixSeq).build();
    try {
        DeleteResponse response = this.client.getKVClient().delete(prefixSeq, deleteOption).get();
        return response.getDeleted();
    } catch (InterruptedException e) {
        throw new ComputerException("Interrupted while deleting with prefix '%s'", e, prefix);
    } catch (ExecutionException e) {
        throw new ComputerException("ExecutionException is thrown while deleting with " + "prefix '%s'", e, prefix);
    }
}
Also used : DeleteOption(io.etcd.jetcd.options.DeleteOption) DeleteResponse(io.etcd.jetcd.kv.DeleteResponse) ExecutionException(java.util.concurrent.ExecutionException) ByteSequence(io.etcd.jetcd.ByteSequence) ComputerException(com.baidu.hugegraph.computer.core.common.exception.ComputerException)

Example 14 with ComputerException

use of com.baidu.hugegraph.computer.core.common.exception.ComputerException in project hugegraph-computer by hugegraph.

the class EtcdClient method put.

/**
 * Put the key value mapping to the map, if the map previously contained a
 * mapping for the key, the old value is replaced by the specified value.
 * @param key key with which the specified value is to be associated
 * @param value value to be associated with the specified key
 */
public void put(String key, byte[] value) {
    E.checkArgument(key != null, "The key can't be null.");
    E.checkArgument(value != null, "The value can't be null.");
    try {
        this.kv.put(ByteSequence.from(key, ENCODING), ByteSequence.from(value)).get();
    } catch (InterruptedException e) {
        throw new ComputerException("Interrupted while putting with key='%s'", e, key);
    } catch (ExecutionException e) {
        throw new ComputerException("Error while putting with key='%s'", e, key);
    }
}
Also used : ExecutionException(java.util.concurrent.ExecutionException) ComputerException(com.baidu.hugegraph.computer.core.common.exception.ComputerException)

Example 15 with ComputerException

use of com.baidu.hugegraph.computer.core.common.exception.ComputerException in project hugegraph-computer by hugegraph.

the class HdfsOutput method init.

@Override
public void init(Config config, int partition) {
    super.init(config, partition);
    try {
        Configuration configuration = new Configuration();
        Short replication = config.get(ComputerOptions.OUTPUT_HDFS_REPLICATION);
        configuration.set(REPLICATION_KEY, String.valueOf(replication));
        this.fs = HdfsOutput.openHDFS(config, configuration);
        this.delimiter = config.get(ComputerOptions.OUTPUT_HDFS_DELIMITER);
        String dir = config.get(ComputerOptions.OUTPUT_HDFS_DIR);
        String jobId = config.get(ComputerOptions.JOB_ID);
        Path hdfsPath = buildPath(dir, jobId, partition);
        this.fileOutputStream = this.fs.create(hdfsPath, true);
    } catch (IOException | InterruptedException e) {
        throw new ComputerException("Failed to init hdfs output on " + "partition [%s]", e, partition);
    }
}
Also used : Path(org.apache.hadoop.fs.Path) Configuration(org.apache.hadoop.conf.Configuration) IOException(java.io.IOException) ComputerException(com.baidu.hugegraph.computer.core.common.exception.ComputerException)

Aggregations

ComputerException (com.baidu.hugegraph.computer.core.common.exception.ComputerException)61 IOException (java.io.IOException)27 ExecutionException (java.util.concurrent.ExecutionException)13 Edges (com.baidu.hugegraph.computer.core.graph.edge.Edges)10 ByteSequence (io.etcd.jetcd.ByteSequence)9 ArrayList (java.util.ArrayList)8 GetResponse (io.etcd.jetcd.kv.GetResponse)7 Vertex (com.baidu.hugegraph.computer.core.graph.vertex.Vertex)6 RandomAccessInput (com.baidu.hugegraph.computer.core.io.RandomAccessInput)6 PartitionStat (com.baidu.hugegraph.computer.core.graph.partition.PartitionStat)5 GetOption (io.etcd.jetcd.options.GetOption)5 Map (java.util.Map)5 Edge (com.baidu.hugegraph.computer.core.graph.edge.Edge)4 BytesInput (com.baidu.hugegraph.computer.core.io.BytesInput)4 ConcurrentHashMap (java.util.concurrent.ConcurrentHashMap)4 Properties (com.baidu.hugegraph.computer.core.graph.properties.Properties)3 Value (com.baidu.hugegraph.computer.core.graph.value.Value)3 MessageStat (com.baidu.hugegraph.computer.core.receiver.MessageStat)3 KeyValue (io.etcd.jetcd.KeyValue)3 DeleteResponse (io.etcd.jetcd.kv.DeleteResponse)3