Search in sources :

Example 46 with ComputerException

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

the class EtcdClient method getWithPrefix.

/**
 * Get expected count of values with the key prefix with prefix. If there
 * is no count of keys, wait at most timeout milliseconds.
 * @param prefix the key prefix
 * @param count the expected count of values to be get
 * @param timeout the max wait time
 * @param logInterval the interval in ms to log message
 * @return the list of values which key with specified prefix
 */
public List<byte[]> getWithPrefix(String prefix, int count, long timeout, long logInterval) {
    E.checkArgumentNotNull(prefix, "The prefix can't be null");
    E.checkArgument(count >= 0, "The count must be >= 0, but got: %s", count);
    E.checkArgument(logInterval >= 0, "The logInterval must be >= 0, but got: %s", logInterval);
    ByteSequence prefixSeq = ByteSequence.from(prefix, ENCODING);
    GetOption getOption = GetOption.newBuilder().withPrefix(prefixSeq).withSortOrder(SortOrder.ASCEND).withLimit(count).build();
    try {
        GetResponse response = this.kv.get(prefixSeq, getOption).get();
        if (response.getCount() == count) {
            return getResponseValues(response);
        } else {
            long revision = response.getHeader().getRevision();
            return this.waitAndPrefixGetFromPutEvent(prefixSeq, count, response.getKvs(), revision, timeout, logInterval);
        }
    } catch (InterruptedException e) {
        throw new ComputerException("Interrupted while getting with prefix='%s', " + "count=%s, timeout=%s", e, prefix, count, timeout);
    } catch (ExecutionException e) {
        throw new ComputerException("Error while getting with prefix='%s', count=%s, " + "timeout=%s", e, prefix, count, timeout);
    }
}
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 47 with ComputerException

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

the class EtcdClient method waitAndGetFromPutEvent.

/**
 * Wait put event.
 * Return the value from event if event triggered in timeout.
 * @throws ComputerException if no event triggered in timeout
 */
private byte[] waitAndGetFromPutEvent(ByteSequence keySeq, long revision, long timeout, long logInterval) throws InterruptedException {
    WaitEvent<byte[]> barrierEvent = new WaitEvent<>();
    Consumer<WatchResponse> consumer = watchResponse -> {
        List<WatchEvent> events = watchResponse.getEvents();
        for (WatchEvent event : events) {
            if (EventType.PUT.equals(event.getEventType())) {
                KeyValue keyValue = event.getKeyValue();
                if (keySeq.equals(keyValue.getKey())) {
                    byte[] result = event.getKeyValue().getValue().getBytes();
                    barrierEvent.signalAll(result);
                    return;
                } else {
                    assert false;
                    throw new ComputerException("Expect event key '%s', found '%s'", keySeq.toString(ENCODING), keyValue.getKey().toString(ENCODING));
                }
            } else {
                assert false;
                throw new ComputerException("Unexpected event type '%s'", event.getEventType());
            }
        }
    };
    WatchOption watchOption = WatchOption.newBuilder().withRevision(revision).withNoDelete(true).build();
    try (Watch.Watcher watcher = this.watch.watch(keySeq, watchOption, consumer)) {
        return barrierEvent.await(timeout, logInterval, () -> {
            LOG.info("Wait for key '{}' with timeout {}ms", keySeq.toString(ENCODING), timeout);
        });
    }
}
Also used : DeleteOption(io.etcd.jetcd.options.DeleteOption) Client(io.etcd.jetcd.Client) WatchEvent(io.etcd.jetcd.watch.WatchEvent) Watch(io.etcd.jetcd.Watch) ArrayList(java.util.ArrayList) DeleteResponse(io.etcd.jetcd.kv.DeleteResponse) Charset(java.nio.charset.Charset) ByteSequence(io.etcd.jetcd.ByteSequence) Map(java.util.Map) GetResponse(io.etcd.jetcd.kv.GetResponse) E(com.baidu.hugegraph.util.E) GetOption(io.etcd.jetcd.options.GetOption) KV(io.etcd.jetcd.KV) Logger(org.slf4j.Logger) EventType(io.etcd.jetcd.watch.WatchEvent.EventType) SortOrder(io.etcd.jetcd.options.GetOption.SortOrder) ConcurrentHashMap(java.util.concurrent.ConcurrentHashMap) KeyValue(io.etcd.jetcd.KeyValue) StandardCharsets(java.nio.charset.StandardCharsets) ExecutionException(java.util.concurrent.ExecutionException) Consumer(java.util.function.Consumer) List(java.util.List) Log(com.baidu.hugegraph.util.Log) WatchResponse(io.etcd.jetcd.watch.WatchResponse) WatchOption(io.etcd.jetcd.options.WatchOption) ComputerException(com.baidu.hugegraph.computer.core.common.exception.ComputerException) BarrierEvent(com.baidu.hugegraph.concurrent.BarrierEvent) VisibleForTesting(com.google.common.annotations.VisibleForTesting) Collections(java.util.Collections) KeyValue(io.etcd.jetcd.KeyValue) Watch(io.etcd.jetcd.Watch) ArrayList(java.util.ArrayList) List(java.util.List) WatchEvent(io.etcd.jetcd.watch.WatchEvent) WatchResponse(io.etcd.jetcd.watch.WatchResponse) ComputerException(com.baidu.hugegraph.computer.core.common.exception.ComputerException) WatchOption(io.etcd.jetcd.options.WatchOption)

Example 48 with ComputerException

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

the class EtcdClient method delete.

/**
 * @return 1 if deleted specified key, 0 if not found specified key
 * The deleted data can be get through revision, if revision is compacted,
 * throw exception "etcdserver: mvcc: required revision has been compacted".
 * @see <a href="https://etcd.io/docs/v3.4.0/op-guide/maintenance/">
 *      Maintenance</a>
 */
public long delete(String key) {
    E.checkArgumentNotNull(key, "The key can't be null");
    ByteSequence keySeq = ByteSequence.from(key, ENCODING);
    try {
        DeleteResponse response = this.client.getKVClient().delete(keySeq).get();
        return response.getDeleted();
    } catch (InterruptedException e) {
        throw new ComputerException("Interrupted while deleting '%s'", e, key);
    } catch (ExecutionException e) {
        throw new ComputerException("Error while deleting '%s'", e, key);
    }
}
Also used : 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 49 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.
 * @param key The key to be found
 * @param throwException whether to throw ComputerException if not found.
 * @return the value of specified key, null if not found and
 * throwException is set false
 * @throws ComputerException if not found and throwException is set true
 */
public byte[] get(String key, boolean throwException) {
    E.checkArgumentNotNull(key, "The key can't be null");
    try {
        ByteSequence keySeq = ByteSequence.from(key, ENCODING);
        GetResponse response = this.kv.get(keySeq).get();
        if (response.getCount() > 0) {
            List<KeyValue> kvs = response.getKvs();
            assert kvs.size() == 1;
            return kvs.get(0).getValue().getBytes();
        } else if (throwException) {
            throw new ComputerException("Can't find value for key='%s'", key);
        } else {
            return null;
        }
    } 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 50 with ComputerException

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

the class FileGraphPartition method input.

protected PartitionStat input(PeekableIterator<KvEntry> vertices, PeekableIterator<KvEntry> edges) {
    try {
        createFile(this.vertexFile);
        createFile(this.edgeFile);
        BufferedFileOutput vertexOut = new BufferedFileOutput(this.vertexFile);
        BufferedFileOutput edgeOut = new BufferedFileOutput(this.edgeFile);
        while (vertices.hasNext()) {
            KvEntry entry = vertices.next();
            Pointer key = entry.key();
            Pointer value = entry.value();
            this.writeVertex(key, value, vertexOut);
            this.writeEdges(key, edges, edgeOut);
        }
        vertexOut.close();
        edgeOut.close();
    } catch (IOException e) {
        throw new ComputerException("Failed to init FileGraphPartition '%s'", e, this.partition);
    }
    return new PartitionStat(this.partition, this.vertexCount, this.edgeCount, 0L);
}
Also used : BufferedFileOutput(com.baidu.hugegraph.computer.core.io.BufferedFileOutput) PartitionStat(com.baidu.hugegraph.computer.core.graph.partition.PartitionStat) KvEntry(com.baidu.hugegraph.computer.core.store.entry.KvEntry) Pointer(com.baidu.hugegraph.computer.core.store.entry.Pointer) 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