use of org.apache.pulsar.metadata.api.GetResult in project incubator-pulsar by apache.
the class PulsarLedgerUnderreplicationManager method getLedgerUnreplicationInfo.
@Override
public UnderreplicatedLedger getLedgerUnreplicationInfo(long ledgerId) throws ReplicationException.UnavailableException {
try {
String path = getUrLedgerPath(ledgerId);
Optional<GetResult> optRes = store.get(path).get();
if (!optRes.isPresent()) {
if (log.isDebugEnabled()) {
log.debug("Ledger: {} is not marked underreplicated", ledgerId);
}
return null;
}
byte[] data = optRes.get().getValue();
UnderreplicatedLedgerFormat.Builder builder = UnderreplicatedLedgerFormat.newBuilder();
TextFormat.merge(new String(data, UTF_8), builder);
UnderreplicatedLedgerFormat underreplicatedLedgerFormat = builder.build();
PulsarUnderreplicatedLedger underreplicatedLedger = new PulsarUnderreplicatedLedger(ledgerId);
List<String> replicaList = underreplicatedLedgerFormat.getReplicaList();
long ctime = (underreplicatedLedgerFormat.hasCtime() ? underreplicatedLedgerFormat.getCtime() : UnderreplicatedLedger.UNASSIGNED_CTIME);
underreplicatedLedger.setCtime(ctime);
underreplicatedLedger.setReplicaList(replicaList);
return underreplicatedLedger;
} catch (ExecutionException ee) {
throw new ReplicationException.UnavailableException("Error contacting with metadata store", ee);
} catch (InterruptedException ie) {
Thread.currentThread().interrupt();
throw new ReplicationException.UnavailableException("Interrupted while connecting metadata store", ie);
} catch (TextFormat.ParseException pe) {
throw new ReplicationException.UnavailableException("Error parsing proto message", pe);
}
}
use of org.apache.pulsar.metadata.api.GetResult in project incubator-pulsar by apache.
the class PulsarLedgerUnderreplicationManager method getReplicasCheckCTime.
@Override
public long getReplicasCheckCTime() throws ReplicationException.UnavailableException {
try {
Optional<GetResult> optRes = store.get(replicasCheckCtimePath).get();
if (!optRes.isPresent()) {
log.warn("placementPolicyCheckCtimeZnode is not yet available");
return -1;
}
byte[] data = optRes.get().getValue();
ReplicasCheckFormat replicasCheckFormat = ReplicasCheckFormat.parseFrom(data);
if (log.isDebugEnabled()) {
log.debug("getReplicasCheckCTime completed successfully");
}
return replicasCheckFormat.hasReplicasCheckCTime() ? replicasCheckFormat.getReplicasCheckCTime() : -1;
} catch (ExecutionException ee) {
throw new ReplicationException.UnavailableException("Error contacting zookeeper", ee);
} catch (InterruptedException ie) {
Thread.currentThread().interrupt();
throw new ReplicationException.UnavailableException("Interrupted while contacting zookeeper", ie);
} catch (InvalidProtocolBufferException ipbe) {
throw new ReplicationException.UnavailableException("Error while parsing ZK protobuf binary data", ipbe);
}
}
use of org.apache.pulsar.metadata.api.GetResult in project incubator-pulsar by apache.
the class ResourceLockImpl method doRevalidate.
private synchronized CompletableFuture<Void> doRevalidate(T newValue) {
if (log.isDebugEnabled()) {
log.debug("doRevalidate with newValue={}, version={}", newValue, version);
}
return store.get(path).thenCompose(optGetResult -> {
if (!optGetResult.isPresent()) {
// The lock just disappeared, try to acquire it again
// Reset the expectation on the version
setVersion(-1L);
return acquireWithNoRevalidation(newValue).thenRun(() -> log.info("Successfully re-acquired missing lock at {}", path));
}
GetResult res = optGetResult.get();
if (!res.getStat().isEphemeral()) {
return FutureUtils.exception(new LockBusyException("Path " + path + " is already created as non-ephemeral"));
}
T existingValue;
try {
existingValue = serde.deserialize(path, res.getValue(), res.getStat());
} catch (Throwable t) {
return FutureUtils.exception(t);
}
synchronized (ResourceLockImpl.this) {
if (newValue.equals(existingValue)) {
if (res.getStat().isCreatedBySelf()) {
// If the new lock belongs to the same session, there's no
// need to recreate it.
version = res.getStat().getVersion();
value = newValue;
return CompletableFuture.completedFuture(null);
} else {
// The lock needs to get recreated since it belong to an earlier
// session which maybe expiring soon
log.info("Deleting stale lock at {}", path);
return store.delete(path, Optional.of(res.getStat().getVersion())).thenRun(() -> setVersion(-1L)).thenCompose(__ -> acquireWithNoRevalidation(newValue)).thenRun(() -> log.info("Successfully re-acquired stale lock at {}", path));
}
}
if (!res.getStat().isCreatedBySelf()) {
return FutureUtils.exception(new LockBusyException("Resource at " + path + " is already locked"));
}
return store.delete(path, Optional.of(res.getStat().getVersion())).thenRun(() -> setVersion(-1L)).thenCompose(__ -> acquireWithNoRevalidation(newValue)).thenRun(() -> log.info("Successfully re-acquired lock at {}", path));
}
});
}
use of org.apache.pulsar.metadata.api.GetResult in project incubator-pulsar by apache.
the class EtcdMetadataStore method handleBatchOperationResult.
private void handleBatchOperationResult(TxnResponse txnResponse, List<MetadataOp> ops) {
executor.execute(() -> {
if (!txnResponse.isSucceeded()) {
if (ops.size() > 1) {
// Retry individually
ops.forEach(o -> batchOperation(Collections.singletonList(o)));
} else {
ops.get(0).getFuture().completeExceptionally(new MetadataStoreException.BadVersionException("Bad version"));
}
return;
}
int getIdx = 0;
int deletedIdx = 0;
int putIdx = 0;
for (MetadataOp op : ops) {
switch(op.getType()) {
case GET:
{
OpGet get = op.asGet();
GetResponse gr = txnResponse.getGetResponses().get(getIdx++);
if (gr.getCount() == 0) {
get.getFuture().complete(Optional.empty());
} else {
KeyValue kv = gr.getKvs().get(0);
boolean isEphemeral = kv.getLease() != 0;
boolean createdBySelf = kv.getLease() == leaseId;
get.getFuture().complete(Optional.of(new GetResult(kv.getValue().getBytes(), new Stat(get.getPath(), kv.getVersion() - 1, 0, 0, isEphemeral, createdBySelf))));
}
break;
}
case PUT:
{
OpPut put = op.asPut();
PutResponse pr = txnResponse.getPutResponses().get(putIdx++);
KeyValue prevKv = pr.getPrevKv();
if (prevKv == null) {
put.getFuture().complete(new Stat(put.getPath(), 0, 0, 0, put.isEphemeral(), true));
} else {
put.getFuture().complete(new Stat(put.getPath(), prevKv.getVersion(), 0, 0, put.isEphemeral(), true));
}
break;
}
case DELETE:
{
OpDelete del = op.asDelete();
DeleteResponse dr = txnResponse.getDeleteResponses().get(deletedIdx++);
if (dr.getDeleted() == 0) {
del.getFuture().completeExceptionally(new MetadataStoreException.NotFoundException());
} else {
del.getFuture().complete(null);
}
break;
}
case GET_CHILDREN:
{
OpGetChildren getChildren = op.asGetChildren();
GetResponse gr = txnResponse.getGetResponses().get(getIdx++);
String basePath = getChildren.getPath() + "/";
Set<String> children = gr.getKvs().stream().map(kv -> kv.getKey().toString(StandardCharsets.UTF_8)).map(p -> p.replace(basePath, "")).map(k -> k.split("/", 2)[0]).collect(Collectors.toCollection(TreeSet::new));
getChildren.getFuture().complete(new ArrayList<>(children));
}
}
}
});
}
use of org.apache.pulsar.metadata.api.GetResult in project incubator-pulsar by apache.
the class PulsarRegistrationManager method readCookie.
@Override
public Versioned<byte[]> readCookie(BookieId bookieId) throws BookieException {
String path = this.cookiePath + "/" + bookieId;
try {
Optional<GetResult> res = store.get(path).get();
if (!res.isPresent()) {
throw new BookieException.CookieNotFoundException(bookieId.toString());
}
// sets stat version from MetadataStore
LongVersion version = new LongVersion(res.get().getStat().getVersion());
return new Versioned<>(res.get().getValue(), version);
} catch (InterruptedException ie) {
Thread.currentThread().interrupt();
throw new BookieException.MetadataStoreException(ie);
} catch (ExecutionException e) {
throw new BookieException.MetadataStoreException(e);
}
}
Aggregations