use of org.apache.commons.lang3.tuple.MutablePair in project pulsar by apache.
the class MLPendingAckReplyCallBack method handleMetadataEntry.
@Override
public void handleMetadataEntry(PendingAckMetadataEntry pendingAckMetadataEntry) {
TxnID txnID = new TxnID(pendingAckMetadataEntry.getTxnidMostBits(), pendingAckMetadataEntry.getTxnidLeastBits());
AckType ackType = pendingAckMetadataEntry.getAckType();
switch(pendingAckMetadataEntry.getPendingAckOp()) {
case ABORT:
pendingAckHandle.handleAbort(txnID, ackType);
break;
case COMMIT:
pendingAckHandle.handleCommit(txnID, ackType, Collections.emptyMap());
break;
case ACK:
if (ackType == AckType.Cumulative) {
PendingAckMetadata pendingAckMetadata = pendingAckMetadataEntry.getPendingAckMetadatasList().get(0);
pendingAckHandle.handleCumulativeAckRecover(txnID, PositionImpl.get(pendingAckMetadata.getLedgerId(), pendingAckMetadata.getEntryId()));
} else {
List<MutablePair<PositionImpl, Integer>> positions = new ArrayList<>();
pendingAckMetadataEntry.getPendingAckMetadatasList().forEach(pendingAckMetadata -> {
if (pendingAckMetadata.getAckSetsCount() == 0) {
positions.add(new MutablePair<>(PositionImpl.get(pendingAckMetadata.getLedgerId(), pendingAckMetadata.getEntryId()), pendingAckMetadata.getBatchSize()));
} else {
PositionImpl position = PositionImpl.get(pendingAckMetadata.getLedgerId(), pendingAckMetadata.getEntryId());
if (pendingAckMetadata.getAckSetsCount() > 0) {
long[] ackSets = new long[pendingAckMetadata.getAckSetsCount()];
for (int i = 0; i < pendingAckMetadata.getAckSetsCount(); i++) {
ackSets[i] = pendingAckMetadata.getAckSetAt(i);
}
position.setAckSet(ackSets);
}
positions.add(new MutablePair<>(position, pendingAckMetadata.getBatchSize()));
}
});
pendingAckHandle.handleIndividualAckRecover(txnID, positions);
}
break;
default:
throw new IllegalStateException("Transaction pending ack replay " + "error with illegal state : " + pendingAckMetadataEntry.getPendingAckOp());
}
}
use of org.apache.commons.lang3.tuple.MutablePair in project pulsar by apache.
the class TransactionProduceTest method getPendingAckCount.
private int getPendingAckCount(String topic, String subscriptionName) throws Exception {
Class<PersistentSubscription> clazz = PersistentSubscription.class;
int pendingAckCount = 0;
for (PulsarService pulsarService : getPulsarServiceList()) {
for (String key : pulsarService.getBrokerService().getTopics().keys()) {
if (key.contains(topic)) {
Field field = clazz.getDeclaredField("pendingAckHandle");
field.setAccessible(true);
PersistentSubscription subscription = (PersistentSubscription) pulsarService.getBrokerService().getTopics().get(key).get().get().getSubscription(subscriptionName);
PendingAckHandleImpl pendingAckHandle = (PendingAckHandleImpl) field.get(subscription);
field = PendingAckHandleImpl.class.getDeclaredField("individualAckPositions");
field.setAccessible(true);
Map<PositionImpl, MutablePair<PositionImpl, Long>> map = (Map<PositionImpl, MutablePair<PositionImpl, Long>>) field.get(pendingAckHandle);
if (map != null) {
pendingAckCount += map.size();
}
}
}
}
log.info("subscriptionName: {}, pendingAckCount: {}", subscriptionName, pendingAckCount);
return pendingAckCount;
}
use of org.apache.commons.lang3.tuple.MutablePair in project pulsar by apache.
the class IsolatedBookieEnsemblePlacementPolicy method getIsolationGroup.
private static Pair<Set<String>, Set<String>> getIsolationGroup(EnsemblePlacementPolicyConfig ensemblePlacementPolicyConfig) {
MutablePair<Set<String>, Set<String>> pair = new MutablePair<>();
String className = IsolatedBookieEnsemblePlacementPolicy.class.getName();
if (ensemblePlacementPolicyConfig.getPolicyClass().getName().equals(className)) {
Map<String, Object> properties = ensemblePlacementPolicyConfig.getProperties();
String primaryIsolationGroupString = castToString(properties.getOrDefault(ISOLATION_BOOKIE_GROUPS, ""));
String secondaryIsolationGroupString = castToString(properties.getOrDefault(SECONDARY_ISOLATION_BOOKIE_GROUPS, ""));
if (!primaryIsolationGroupString.isEmpty()) {
pair.setLeft(new HashSet(Arrays.asList(primaryIsolationGroupString.split(","))));
}
if (!secondaryIsolationGroupString.isEmpty()) {
pair.setRight(new HashSet(Arrays.asList(secondaryIsolationGroupString.split(","))));
}
}
return pair;
}
use of org.apache.commons.lang3.tuple.MutablePair in project pulsar by apache.
the class PersistentSubscriptionTest method testCanAcknowledgeAndAbortForTransaction.
@Test
public void testCanAcknowledgeAndAbortForTransaction() throws Exception {
List<MutablePair<PositionImpl, Integer>> positionsPair = new ArrayList<>();
positionsPair.add(new MutablePair<>(new PositionImpl(2, 1), 0));
positionsPair.add(new MutablePair<>(new PositionImpl(2, 3), 0));
positionsPair.add(new MutablePair<>(new PositionImpl(2, 5), 0));
doAnswer((invocationOnMock) -> {
((AsyncCallbacks.DeleteCallback) invocationOnMock.getArguments()[1]).deleteComplete(invocationOnMock.getArguments()[2]);
return null;
}).when(cursorMock).asyncDelete(any(List.class), any(AsyncCallbacks.DeleteCallback.class), any());
doReturn(CommandSubscribe.SubType.Exclusive).when(consumerMock).subType();
Awaitility.await().until(() -> {
try {
persistentSubscription.addConsumer(consumerMock);
return true;
} catch (Exception e) {
return false;
}
});
// Single ack for txn1
persistentSubscription.transactionIndividualAcknowledge(txnID1, positionsPair);
List<PositionImpl> positions = new ArrayList<>();
positions.add(new PositionImpl(1, 100));
// Cumulative ack for txn1
persistentSubscription.transactionCumulativeAcknowledge(txnID1, positions).get();
positions.clear();
positions.add(new PositionImpl(2, 1));
// Can not single ack message already acked.
try {
persistentSubscription.transactionIndividualAcknowledge(txnID2, positionsPair).get();
fail("Single acknowledge for transaction2 should fail. ");
} catch (ExecutionException e) {
assertEquals(e.getCause().getMessage(), "[persistent://prop/use/ns-abc/successTopic][subscriptionName] " + "Transaction:(1,2) try to ack message:2:1 in pending ack status.");
}
positions.clear();
positions.add(new PositionImpl(2, 50));
// Can not cumulative ack message for another txn.
try {
persistentSubscription.transactionCumulativeAcknowledge(txnID2, positions).get();
fail("Cumulative acknowledge for transaction2 should fail. ");
} catch (ExecutionException e) {
assertTrue(e.getCause() instanceof TransactionConflictException);
assertEquals(e.getCause().getMessage(), "[persistent://prop/use/ns-abc/successTopic]" + "[subscriptionName] Transaction:(1,2) try to cumulative batch ack position: " + "2:50 within range of current currentPosition: 1:100");
}
List<Position> positionList = new ArrayList<>();
positionList.add(new PositionImpl(1, 1));
positionList.add(new PositionImpl(1, 3));
positionList.add(new PositionImpl(1, 5));
positionList.add(new PositionImpl(3, 1));
positionList.add(new PositionImpl(3, 3));
positionList.add(new PositionImpl(3, 5));
// Acknowledge from normal consumer will succeed ignoring message acked by ongoing transaction.
persistentSubscription.acknowledgeMessage(positionList, AckType.Individual, Collections.emptyMap());
// Abort txn.
persistentSubscription.endTxn(txnID1.getMostSigBits(), txnID2.getLeastSigBits(), TxnAction.ABORT_VALUE, -1);
positions.clear();
positions.add(new PositionImpl(2, 50));
// Retry above ack, will succeed. As abort has clear pending_ack for those messages.
persistentSubscription.transactionCumulativeAcknowledge(txnID2, positions);
positionsPair.clear();
positionsPair.add(new MutablePair(new PositionImpl(2, 1), 0));
persistentSubscription.transactionIndividualAcknowledge(txnID2, positionsPair);
}
use of org.apache.commons.lang3.tuple.MutablePair in project lobcder by skoulouzis.
the class WebDataResource method createResouses.
private Map<Long, Pair<WebDataFileResource, Long>> createResouses(Request request) throws SQLException, UnsupportedEncodingException, NotAuthorizedException, NoSuchAlgorithmException, IOException {
Map<Long, Pair<WebDataFileResource, Long>> resources = null;
try (Connection connection = getCatalogue().getConnection()) {
Map<String, FileItem> files = request.getFiles();
Collection<FileItem> fileItems = files.values();
resources = new HashMap<>();
WebDataFileResource resource = null;
for (FileItem fi : fileItems) {
Long pdriGroupid;
Path newPath = Path.path(getPath(), fi.getName());
LogicalData fileLogicalData = getCatalogue().getLogicalDataByPath(newPath, connection);
String contentType = mimeTypeMap.get(FilenameUtils.getExtension(fi.getName()));
if (fileLogicalData != null) {
Permissions p = getCatalogue().getPermissions(fileLogicalData.getUid(), fileLogicalData.getOwner(), connection);
if (!getPrincipal().canWrite(p)) {
throw new NotAuthorizedException(this);
}
fileLogicalData.setLength(fi.getSize());
fileLogicalData.setModifiedDate(System.currentTimeMillis());
fileLogicalData.setLastAccessDate(fileLogicalData.getModifiedDate());
fileLogicalData.addContentType(contentType);
pdriGroupid = fileLogicalData.getPdriGroupId();
resource = new WebDataFileResource(fileLogicalData, Path.path(getPath(), fi.getName()), getCatalogue(), authList);
} else {
fileLogicalData = new LogicalData();
fileLogicalData.setName(fi.getName());
fileLogicalData.setParentRef(getLogicalData().getUid());
fileLogicalData.setType(Constants.LOGICAL_FILE);
fileLogicalData.setOwner(getPrincipal().getUserId());
fileLogicalData.setLength(fi.getSize());
fileLogicalData.setCreateDate(System.currentTimeMillis());
fileLogicalData.setModifiedDate(System.currentTimeMillis());
fileLogicalData.setLastAccessDate(System.currentTimeMillis());
fileLogicalData.setTtlSec(getLogicalData().getTtlSec());
fileLogicalData.addContentType(contentType);
pdriGroupid = getCatalogue().associateLogicalDataAndPdriGroup(fileLogicalData, connection);
getCatalogue().setPreferencesOn(fileLogicalData.getUid(), getLogicalData().getUid(), connection);
List<String> pref = getLogicalData().getDataLocationPreferences();
fileLogicalData.setDataLocationPreferences(pref);
resource = new WebDataFileResource(fileLogicalData, Path.path(getPath(), fi.getName()), getCatalogue(), authList);
}
MutablePair<WebDataFileResource, Long> pair = new MutablePair<>();
pair.setRight(pdriGroupid);
pair.setLeft(resource);
resources.put(Long.valueOf(resource.getUniqueId()), pair);
}
connection.commit();
connection.close();
}
return resources;
}
Aggregations