Search in sources :

Example 41 with MutablePair

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());
    }
}
Also used : MutablePair(org.apache.commons.lang3.tuple.MutablePair) TxnID(org.apache.pulsar.client.api.transaction.TxnID) AckType(org.apache.pulsar.common.api.proto.CommandAck.AckType) PendingAckMetadata(org.apache.pulsar.broker.transaction.pendingack.proto.PendingAckMetadata) PositionImpl(org.apache.bookkeeper.mledger.impl.PositionImpl) ArrayList(java.util.ArrayList)

Example 42 with MutablePair

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;
}
Also used : Field(java.lang.reflect.Field) MutablePair(org.apache.commons.lang3.tuple.MutablePair) PulsarService(org.apache.pulsar.broker.PulsarService) PendingAckHandleImpl(org.apache.pulsar.broker.transaction.pendingack.impl.PendingAckHandleImpl) PositionImpl(org.apache.bookkeeper.mledger.impl.PositionImpl) PersistentSubscription(org.apache.pulsar.broker.service.persistent.PersistentSubscription) Map(java.util.Map)

Example 43 with MutablePair

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;
}
Also used : MutablePair(org.apache.commons.lang3.tuple.MutablePair) HashSet(java.util.HashSet) Set(java.util.Set) HashSet(java.util.HashSet)

Example 44 with MutablePair

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);
}
Also used : Position(org.apache.bookkeeper.mledger.Position) PositionImpl(org.apache.bookkeeper.mledger.impl.PositionImpl) ArrayList(java.util.ArrayList) TransactionConflictException(org.apache.pulsar.transaction.common.exception.TransactionConflictException) TransactionConflictException(org.apache.pulsar.transaction.common.exception.TransactionConflictException) ExecutionException(java.util.concurrent.ExecutionException) MutablePair(org.apache.commons.lang3.tuple.MutablePair) List(java.util.List) ArrayList(java.util.ArrayList) ExecutionException(java.util.concurrent.ExecutionException) Test(org.testng.annotations.Test)

Example 45 with MutablePair

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;
}
Also used : Path(io.milton.common.Path) NotAuthorizedException(io.milton.http.exceptions.NotAuthorizedException) MutablePair(org.apache.commons.lang3.tuple.MutablePair) Permissions(nl.uva.cs.lobcder.auth.Permissions) MutablePair(org.apache.commons.lang3.tuple.MutablePair) Pair(org.apache.commons.lang3.tuple.Pair)

Aggregations

MutablePair (org.apache.commons.lang3.tuple.MutablePair)116 Pair (org.apache.commons.lang3.tuple.Pair)49 ArrayList (java.util.ArrayList)32 Test (org.junit.Test)32 HashMap (java.util.HashMap)29 Message (com.microsoft.azure.sdk.iot.device.Message)27 IotHubTransportMessage (com.microsoft.azure.sdk.iot.device.transport.IotHubTransportMessage)27 ConcurrentLinkedQueue (java.util.concurrent.ConcurrentLinkedQueue)23 List (java.util.List)20 MqttDeviceTwin (com.microsoft.azure.sdk.iot.device.transport.mqtt.MqttDeviceTwin)17 Map (java.util.Map)14 IOException (java.io.IOException)13 WalkPosition (org.openbw.bwapi4j.WalkPosition)9 MiniTile (bwem.tile.MiniTile)8 DeviceOperations (com.microsoft.azure.sdk.iot.device.DeviceTwin.DeviceOperations)8 AreaId (bwem.area.typedef.AreaId)7 Collectors (java.util.stream.Collectors)7 PositionImpl (org.apache.bookkeeper.mledger.impl.PositionImpl)7 HashSet (java.util.HashSet)6 TileImpl (bwem.tile.TileImpl)5