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);
}
}
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);
});
}
}
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);
}
}
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);
}
}
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);
}
Aggregations