Search in sources :

Example 21 with FutureCallback

use of com.google.common.util.concurrent.FutureCallback in project controller by opendaylight.

the class ConcurrentDOMDataBroker method doCanCommit.

private void doCanCommit(final AsyncNotifyingSettableFuture clientSubmitFuture, final DOMDataTreeWriteTransaction transaction, final Collection<DOMStoreThreePhaseCommitCohort> cohorts) {
    final long startTime = System.nanoTime();
    final Iterator<DOMStoreThreePhaseCommitCohort> cohortIterator = cohorts.iterator();
    // Not using Futures.allAsList here to avoid its internal overhead.
    FutureCallback<Boolean> futureCallback = new FutureCallback<Boolean>() {

        @Override
        public void onSuccess(final Boolean result) {
            if (result == null || !result) {
                handleException(clientSubmitFuture, transaction, cohorts, CAN_COMMIT, CAN_COMMIT_ERROR_MAPPER, new TransactionCommitFailedException("Can Commit failed, no detailed cause available."));
            } else if (!cohortIterator.hasNext()) {
                // All cohorts completed successfully - we can move on to the preCommit phase
                doPreCommit(startTime, clientSubmitFuture, transaction, cohorts);
            } else {
                Futures.addCallback(cohortIterator.next().canCommit(), this, MoreExecutors.directExecutor());
            }
        }

        @Override
        public void onFailure(final Throwable failure) {
            handleException(clientSubmitFuture, transaction, cohorts, CAN_COMMIT, CAN_COMMIT_ERROR_MAPPER, failure);
        }
    };
    ListenableFuture<Boolean> canCommitFuture = cohortIterator.next().canCommit();
    Futures.addCallback(canCommitFuture, futureCallback, MoreExecutors.directExecutor());
}
Also used : TransactionCommitFailedException(org.opendaylight.mdsal.common.api.TransactionCommitFailedException) DOMStoreThreePhaseCommitCohort(org.opendaylight.mdsal.dom.spi.store.DOMStoreThreePhaseCommitCohort) FutureCallback(com.google.common.util.concurrent.FutureCallback)

Example 22 with FutureCallback

use of com.google.common.util.concurrent.FutureCallback in project openflowplugin by opendaylight.

the class DeviceContextImpl method handlePacketInMessage.

private void handlePacketInMessage(final PacketIn packetIn, final Class<?> implementedInterface, final Match match) {
    messageSpy.spyMessage(implementedInterface, MessageSpy.StatisticsGroup.FROM_SWITCH);
    final ConnectionAdapter connectionAdapter = getPrimaryConnectionContext().getConnectionAdapter();
    if (packetIn == null) {
        LOG.debug("Received a null packet from switch {}", connectionAdapter.getRemoteAddress());
        messageSpy.spyMessage(implementedInterface, MessageSpy.StatisticsGroup.FROM_SWITCH_TRANSLATE_SRC_FAILURE);
        return;
    }
    final OpenflowVersion openflowVersion = OpenflowVersion.get(deviceInfo.getVersion());
    // Try to get ingress from match
    final NodeConnectorRef nodeConnectorRef = Objects.nonNull(packetIn.getIngress()) ? packetIn.getIngress() : Optional.ofNullable(match).map(Match::getInPort).map(nodeConnectorId -> InventoryDataServiceUtil.portNumberfromNodeConnectorId(openflowVersion, nodeConnectorId)).map(portNumber -> InventoryDataServiceUtil.nodeConnectorRefFromDatapathIdPortno(deviceInfo.getDatapathId(), portNumber, openflowVersion)).orElse(null);
    messageSpy.spyMessage(implementedInterface, MessageSpy.StatisticsGroup.FROM_SWITCH_TRANSLATE_OUT_SUCCESS);
    if (!packetInLimiter.acquirePermit()) {
        LOG.debug("Packet limited");
        // TODO: save packet into emergency slot if possible
        messageSpy.spyMessage(implementedInterface, MessageSpy.StatisticsGroup.FROM_SWITCH_PACKET_IN_LIMIT_REACHED_AND_DROPPED);
        return;
    }
    final ListenableFuture<?> offerNotification = notificationPublishService.offerNotification(new PacketReceivedBuilder(packetIn).setIngress(nodeConnectorRef).setMatch(MatchUtil.transformMatch(match, org.opendaylight.yang.gen.v1.urn.opendaylight.packet.service.rev130709.packet.received.Match.class)).build());
    if (NotificationPublishService.REJECTED.equals(offerNotification)) {
        LOG.debug("notification offer rejected");
        messageSpy.spyMessage(implementedInterface, MessageSpy.StatisticsGroup.FROM_SWITCH_NOTIFICATION_REJECTED);
        packetInLimiter.drainLowWaterMark();
        packetInLimiter.releasePermit();
        return;
    }
    Futures.addCallback(offerNotification, new FutureCallback<Object>() {

        @Override
        public void onSuccess(final Object result) {
            messageSpy.spyMessage(implementedInterface, MessageSpy.StatisticsGroup.FROM_SWITCH_PUBLISHED_SUCCESS);
            packetInLimiter.releasePermit();
        }

        @Override
        public void onFailure(final Throwable throwable) {
            messageSpy.spyMessage(implementedInterface, MessageSpy.StatisticsGroup.FROM_SWITCH_NOTIFICATION_REJECTED);
            LOG.debug("notification offer failed: {}", throwable.getMessage());
            LOG.trace("notification offer failed..", throwable);
            packetInLimiter.releasePermit();
        }
    }, MoreExecutors.directExecutor());
}
Also used : MultipartWriterProvider(org.opendaylight.openflowplugin.impl.datastore.MultipartWriterProvider) FlowCapableNodeConnectorStatisticsData(org.opendaylight.yang.gen.v1.urn.opendaylight.port.statistics.rev131214.FlowCapableNodeConnectorStatisticsData) DeviceMeterRegistry(org.opendaylight.openflowplugin.api.openflow.registry.meter.DeviceMeterRegistry) ServiceGroupIdentifier(org.opendaylight.mdsal.singleton.common.api.ServiceGroupIdentifier) Future(java.util.concurrent.Future) Error(org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.Error) PacketInMessage(org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.PacketInMessage) NodeConnectorKey(org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.node.NodeConnectorKey) ExtensionConverterProvider(org.opendaylight.openflowplugin.extension.api.core.extension.ExtensionConverterProvider) NodeRef(org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.NodeRef) ContextChainState(org.opendaylight.openflowplugin.api.openflow.lifecycle.ContextChainState) PortStatusMessage(org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.PortStatusMessage) PortGrouping(org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.PortGrouping) DataBroker(org.opendaylight.controller.md.sal.binding.api.DataBroker) FlowCapableNodeConnectorStatisticsDataBuilder(org.opendaylight.yang.gen.v1.urn.opendaylight.port.statistics.rev131214.FlowCapableNodeConnectorStatisticsDataBuilder) ConnectionAdapter(org.opendaylight.openflowjava.protocol.api.connection.ConnectionAdapter) PacketReceivedBuilder(org.opendaylight.yang.gen.v1.urn.opendaylight.packet.service.rev130709.PacketReceivedBuilder) AbstractDeviceInitializer(org.opendaylight.openflowplugin.impl.device.initialization.AbstractDeviceInitializer) PacketReceived(org.opendaylight.yang.gen.v1.urn.opendaylight.packet.service.rev130709.PacketReceived) DataContainer(org.opendaylight.yangtools.yang.binding.DataContainer) MultiMsgCollectorImpl(org.opendaylight.openflowplugin.impl.device.listener.MultiMsgCollectorImpl) ConversionException(org.opendaylight.openflowplugin.extension.api.exception.ConversionException) TransactionChainManager(org.opendaylight.openflowplugin.common.txchain.TransactionChainManager) DeviceInitializerProvider(org.opendaylight.openflowplugin.impl.device.initialization.DeviceInitializerProvider) FlowCapableNode(org.opendaylight.yang.gen.v1.urn.opendaylight.flow.inventory.rev130819.FlowCapableNode) OpenflowVersion(org.opendaylight.openflowplugin.api.openflow.md.util.OpenflowVersion) PacketIn(org.opendaylight.yang.gen.v1.urn.opendaylight.packet.service.rev130709.PacketIn) Xid(org.opendaylight.openflowplugin.api.openflow.device.Xid) OfHeader(org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.OfHeader) Nullable(javax.annotation.Nullable) ContextChainMastershipState(org.opendaylight.openflowplugin.api.openflow.lifecycle.ContextChainMastershipState) AbstractRequestContext(org.opendaylight.openflowplugin.impl.rpc.AbstractRequestContext) NodeConnectorRef(org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.NodeConnectorRef) Match(org.opendaylight.yang.gen.v1.urn.opendaylight.model.match.types.rev131026.Match) FutureCallback(com.google.common.util.concurrent.FutureCallback) ExecutionException(java.util.concurrent.ExecutionException) Futures(com.google.common.util.concurrent.Futures) InstanceIdentifier(org.opendaylight.yangtools.yang.binding.InstanceIdentifier) ExtensionConverterProviderKeeper(org.opendaylight.openflowplugin.extension.api.ExtensionConverterProviderKeeper) KeyedInstanceIdentifier(org.opendaylight.yangtools.yang.binding.KeyedInstanceIdentifier) TranslatorKey(org.opendaylight.openflowplugin.api.openflow.md.core.TranslatorKey) ConvertorMessageFromOFJava(org.opendaylight.openflowplugin.extension.api.ConvertorMessageFromOFJava) ContextChainHolder(org.opendaylight.openflowplugin.api.openflow.lifecycle.ContextChainHolder) DeviceFlowRegistryImpl(org.opendaylight.openflowplugin.impl.registry.flow.DeviceFlowRegistryImpl) NodeConnectorBuilder(org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.node.NodeConnectorBuilder) DeviceFlowRegistry(org.opendaylight.openflowplugin.api.openflow.registry.flow.DeviceFlowRegistry) NodeConnector(org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.node.NodeConnector) LoggerFactory(org.slf4j.LoggerFactory) TimeoutException(java.util.concurrent.TimeoutException) NotificationPublishService(org.opendaylight.controller.md.sal.binding.api.NotificationPublishService) ContextChainMastershipWatcher(org.opendaylight.openflowplugin.api.openflow.lifecycle.ContextChainMastershipWatcher) ExperimenterMessageFromDevBuilder(org.opendaylight.yang.gen.v1.urn.opendaylight.experimenter.message.service.rev151020.ExperimenterMessageFromDevBuilder) MessagePath(org.opendaylight.openflowplugin.extension.api.path.MessagePath) MessageTranslator(org.opendaylight.openflowplugin.api.openflow.device.MessageTranslator) DeviceContext(org.opendaylight.openflowplugin.api.openflow.device.DeviceContext) DataObject(org.opendaylight.yangtools.yang.binding.DataObject) Collection(java.util.Collection) LogicalDatastoreType(org.opendaylight.controller.md.sal.common.api.data.LogicalDatastoreType) ExperimenterDataOfChoice(org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.experimenter.core.ExperimenterDataOfChoice) Objects(java.util.Objects) DeviceInfo(org.opendaylight.openflowplugin.api.openflow.device.DeviceInfo) List(java.util.List) ContextChain(org.opendaylight.openflowplugin.api.openflow.lifecycle.ContextChain) ExperimenterMessage(org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.ExperimenterMessage) MessageSpy(org.opendaylight.openflowplugin.api.openflow.statistics.ofpspecific.MessageSpy) HashedWheelTimer(io.netty.util.HashedWheelTimer) MultiMsgCollector(org.opendaylight.openflowplugin.api.openflow.device.handlers.MultiMsgCollector) Optional(java.util.Optional) DeviceMeterRegistryImpl(org.opendaylight.openflowplugin.impl.registry.meter.DeviceMeterRegistryImpl) InventoryDataServiceUtil(org.opendaylight.openflowplugin.openflow.md.util.InventoryDataServiceUtil) FlowRemoved(org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.FlowRemoved) SuppressFBWarnings(edu.umd.cs.findbugs.annotations.SuppressFBWarnings) MoreExecutors(com.google.common.util.concurrent.MoreExecutors) FlowCapableNodeConnector(org.opendaylight.yang.gen.v1.urn.opendaylight.flow.inventory.rev130819.FlowCapableNodeConnector) ListenableFuture(com.google.common.util.concurrent.ListenableFuture) ConvertorExecutor(org.opendaylight.openflowplugin.openflow.md.core.sal.convertor.ConvertorExecutor) AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) MultipartWriterProviderFactory(org.opendaylight.openflowplugin.impl.datastore.MultipartWriterProviderFactory) DeviceState(org.opendaylight.openflowplugin.api.openflow.device.DeviceState) HashSet(java.util.HashSet) ReadOnlyTransaction(org.opendaylight.controller.md.sal.binding.api.ReadOnlyTransaction) RequestContextUtil(org.opendaylight.openflowplugin.impl.services.util.RequestContextUtil) PortReason(org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.common.types.rev130731.PortReason) Nonnull(javax.annotation.Nonnull) PortStatus(org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.PortStatus) MessageTypeKey(org.opendaylight.openflowjava.protocol.api.keys.MessageTypeKey) ConnectionContext(org.opendaylight.openflowplugin.api.openflow.connection.ConnectionContext) DeviceGroupRegistryImpl(org.opendaylight.openflowplugin.impl.registry.group.DeviceGroupRegistryImpl) Logger(org.slf4j.Logger) OFConstants(org.opendaylight.openflowplugin.api.OFConstants) TimeUnit(java.util.concurrent.TimeUnit) DeviceGroupRegistry(org.opendaylight.openflowplugin.api.openflow.registry.group.DeviceGroupRegistry) ExperimenterMessageOfChoice(org.opendaylight.yang.gen.v1.urn.opendaylight.openflowplugin.experimenter.types.rev151020.experimenter.core.message.ExperimenterMessageOfChoice) RequestContext(org.opendaylight.openflowplugin.api.openflow.device.RequestContext) TranslatorLibrary(org.opendaylight.openflowplugin.api.openflow.device.TranslatorLibrary) VisibleForTesting(com.google.common.annotations.VisibleForTesting) Collections(java.util.Collections) MatchUtil(org.opendaylight.openflowplugin.impl.util.MatchUtil) NodeConnectorRef(org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.NodeConnectorRef) OpenflowVersion(org.opendaylight.openflowplugin.api.openflow.md.util.OpenflowVersion) Match(org.opendaylight.yang.gen.v1.urn.opendaylight.model.match.types.rev131026.Match) PacketReceivedBuilder(org.opendaylight.yang.gen.v1.urn.opendaylight.packet.service.rev130709.PacketReceivedBuilder) ConnectionAdapter(org.opendaylight.openflowjava.protocol.api.connection.ConnectionAdapter) DataObject(org.opendaylight.yangtools.yang.binding.DataObject)

Example 23 with FutureCallback

use of com.google.common.util.concurrent.FutureCallback in project openflowplugin by opendaylight.

the class SalTableServiceImplTest method setup.

@Override
public void setup() {
    handleResultFuture = SettableFuture.create();
    when(mockedRequestContext.getFuture()).thenReturn(handleResultFuture);
    Mockito.doAnswer((Answer<Void>) invocation -> {
        final FutureCallback<OfHeader> callback = (FutureCallback<OfHeader>) invocation.getArguments()[2];
        callback.onSuccess(null);
        return null;
    }).when(mockedOutboundQueue).commitEntry(Matchers.anyLong(), Matchers.<OfHeader>any(), Matchers.<FutureCallback<OfHeader>>any());
    final ConvertorManager convertorManager = ConvertorManagerFactory.createDefaultManager();
    salTableService = new SalTableServiceImpl(mockedRequestContextStack, mockedDeviceContext, convertorManager, MultipartWriterProviderFactory.createDefaultProvider(mockedDeviceContext));
}
Also used : UpdateTableInput(org.opendaylight.yang.gen.v1.urn.opendaylight.table.service.rev131026.UpdateTableInput) TableFeaturesBuilder(org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.multipart.reply.multipart.reply.body.multipart.reply.table.features._case.multipart.reply.table.features.TableFeaturesBuilder) TableFeatures(org.opendaylight.yang.gen.v1.urn.opendaylight.table.types.rev131026.table.features.TableFeatures) RpcResult(org.opendaylight.yangtools.yang.common.RpcResult) ServiceMocking(org.opendaylight.openflowplugin.impl.services.ServiceMocking) UpdatedTableBuilder(org.opendaylight.yang.gen.v1.urn.opendaylight.table.service.rev131026.table.update.UpdatedTableBuilder) Matchers(org.mockito.Matchers) Mock(org.mockito.Mock) RpcProviderRegistry(org.opendaylight.controller.sal.binding.api.RpcProviderRegistry) MultipartWriterProviderFactory(org.opendaylight.openflowplugin.impl.datastore.MultipartWriterProviderFactory) MultipartReplyTableFeaturesCaseBuilder(org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.multipart.reply.multipart.reply.body.MultipartReplyTableFeaturesCaseBuilder) SettableFuture(com.google.common.util.concurrent.SettableFuture) Answer(org.mockito.stubbing.Answer) MultipartReplyTableFeaturesBuilder(org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.multipart.reply.multipart.reply.body.multipart.reply.table.features._case.MultipartReplyTableFeaturesBuilder) Future(java.util.concurrent.Future) OfHeader(org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.OfHeader) ConvertorManagerFactory(org.opendaylight.openflowplugin.openflow.md.core.sal.convertor.ConvertorManagerFactory) UpdateTableInputBuilder(org.opendaylight.yang.gen.v1.urn.opendaylight.table.service.rev131026.UpdateTableInputBuilder) UpdateTableOutput(org.opendaylight.yang.gen.v1.urn.opendaylight.table.service.rev131026.UpdateTableOutput) Test(org.junit.Test) MultipartReplyMessageBuilder(org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.MultipartReplyMessageBuilder) Mockito.when(org.mockito.Mockito.when) FutureCallback(com.google.common.util.concurrent.FutureCallback) Mockito.verify(org.mockito.Mockito.verify) ExecutionException(java.util.concurrent.ExecutionException) Mockito(org.mockito.Mockito) List(java.util.List) ConvertorManager(org.opendaylight.openflowplugin.openflow.md.core.sal.convertor.ConvertorManager) TableFeatureProperties(org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.table.features.properties.grouping.TableFeatureProperties) EventIdentifier(org.opendaylight.openflowplugin.api.openflow.statistics.ofpspecific.EventIdentifier) MultipartReply(org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.MultipartReply) RpcResultBuilder(org.opendaylight.yangtools.yang.common.RpcResultBuilder) MultipartType(org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.common.types.rev130731.MultipartType) Assert(org.junit.Assert) Collections(java.util.Collections) OfHeader(org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.OfHeader) ConvertorManager(org.opendaylight.openflowplugin.openflow.md.core.sal.convertor.ConvertorManager) FutureCallback(com.google.common.util.concurrent.FutureCallback)

Example 24 with FutureCallback

use of com.google.common.util.concurrent.FutureCallback in project openflowplugin by opendaylight.

the class DeviceFlowRegistryImpl method fillFromDatastore.

private CheckedFuture<Optional<FlowCapableNode>, ReadFailedException> fillFromDatastore(final LogicalDatastoreType logicalDatastoreType, final InstanceIdentifier<FlowCapableNode> path) {
    // Create new read-only transaction
    final ReadOnlyTransaction transaction = dataBroker.newReadOnlyTransaction();
    // Bail out early if transaction is null
    if (transaction == null) {
        return Futures.immediateFailedCheckedFuture(new ReadFailedException("Read transaction is null"));
    }
    // Prepare read operation from datastore for path
    final CheckedFuture<Optional<FlowCapableNode>, ReadFailedException> future = transaction.read(logicalDatastoreType, path);
    // Bail out early if future is null
    if (future == null) {
        return Futures.immediateFailedCheckedFuture(new ReadFailedException("Future from read transaction is null"));
    }
    Futures.addCallback(future, new FutureCallback<Optional<FlowCapableNode>>() {

        @Override
        public void onSuccess(@Nonnull Optional<FlowCapableNode> result) {
            result.asSet().stream().filter(Objects::nonNull).filter(flowCapableNode -> Objects.nonNull(flowCapableNode.getTable())).flatMap(flowCapableNode -> flowCapableNode.getTable().stream()).filter(Objects::nonNull).filter(table -> Objects.nonNull(table.getFlow())).flatMap(table -> table.getFlow().stream()).filter(Objects::nonNull).filter(flow -> Objects.nonNull(flow.getId())).forEach(flowConsumer);
            // After we are done with reading from datastore, close the transaction
            transaction.close();
        }

        @Override
        public void onFailure(Throwable throwable) {
            // Even when read operation failed, close the transaction
            transaction.close();
        }
    }, MoreExecutors.directExecutor());
    return future;
}
Also used : MoreExecutors(com.google.common.util.concurrent.MoreExecutors) Arrays(java.util.Arrays) ListenableFuture(com.google.common.util.concurrent.ListenableFuture) DeviceFlowRegistry(org.opendaylight.openflowplugin.api.openflow.registry.flow.DeviceFlowRegistry) LoggerFactory(org.slf4j.LoggerFactory) CheckedFuture(com.google.common.util.concurrent.CheckedFuture) Flow(org.opendaylight.yang.gen.v1.urn.opendaylight.flow.inventory.rev130819.tables.table.Flow) FlowCapableNode(org.opendaylight.yang.gen.v1.urn.opendaylight.flow.inventory.rev130819.FlowCapableNode) FlowId(org.opendaylight.yang.gen.v1.urn.opendaylight.flow.inventory.rev130819.FlowId) ArrayList(java.util.ArrayList) ReadOnlyTransaction(org.opendaylight.controller.md.sal.binding.api.ReadOnlyTransaction) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) NodeKey(org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.nodes.NodeKey) Optional(com.google.common.base.Optional) Map(java.util.Map) FlowDescriptor(org.opendaylight.openflowplugin.api.openflow.registry.flow.FlowDescriptor) Nonnull(javax.annotation.Nonnull) Node(org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.nodes.Node) FlowRegistryKey(org.opendaylight.openflowplugin.api.openflow.registry.flow.FlowRegistryKey) BiMap(com.google.common.collect.BiMap) Logger(org.slf4j.Logger) Iterator(java.util.Iterator) LogicalDatastoreType(org.opendaylight.controller.md.sal.common.api.data.LogicalDatastoreType) ThreadSafe(javax.annotation.concurrent.ThreadSafe) Maps(com.google.common.collect.Maps) FutureCallback(com.google.common.util.concurrent.FutureCallback) DataBroker(org.opendaylight.controller.md.sal.binding.api.DataBroker) Objects(java.util.Objects) Consumer(java.util.function.Consumer) GeneralAugMatchNodesNodeTableFlow(org.opendaylight.yang.gen.v1.urn.opendaylight.openflowplugin.extension.general.rev140714.GeneralAugMatchNodesNodeTableFlow) HashBiMap(com.google.common.collect.HashBiMap) Futures(com.google.common.util.concurrent.Futures) List(java.util.List) InstanceIdentifier(org.opendaylight.yangtools.yang.binding.InstanceIdentifier) ReadFailedException(org.opendaylight.controller.md.sal.common.api.data.ReadFailedException) KeyedInstanceIdentifier(org.opendaylight.yangtools.yang.binding.KeyedInstanceIdentifier) VisibleForTesting(com.google.common.annotations.VisibleForTesting) ReadFailedException(org.opendaylight.controller.md.sal.common.api.data.ReadFailedException) Optional(com.google.common.base.Optional) FlowCapableNode(org.opendaylight.yang.gen.v1.urn.opendaylight.flow.inventory.rev130819.FlowCapableNode) ReadOnlyTransaction(org.opendaylight.controller.md.sal.binding.api.ReadOnlyTransaction) Objects(java.util.Objects)

Example 25 with FutureCallback

use of com.google.common.util.concurrent.FutureCallback in project cassandra by apache.

the class ViewBuilder method build.

private synchronized void build() {
    if (isStopped) {
        logger.debug("Stopped build for view({}.{}) after covering {} keys", ksName, view.name, keysBuilt);
        return;
    }
    // Get the local ranges for which the view hasn't already been built nor it's building
    RangesAtEndpoint replicatedRanges = StorageService.instance.getLocalReplicas(ksName);
    Replicas.temporaryAssertFull(replicatedRanges);
    Set<Range<Token>> newRanges = replicatedRanges.ranges().stream().map(r -> r.subtractAll(builtRanges)).flatMap(Set::stream).map(r -> r.subtractAll(pendingRanges.keySet())).flatMap(Set::stream).collect(Collectors.toSet());
    // If there are no new nor pending ranges we should finish the build
    if (newRanges.isEmpty() && pendingRanges.isEmpty()) {
        finish();
        return;
    }
    // Split the new local ranges and add them to the pending set
    DatabaseDescriptor.getPartitioner().splitter().map(s -> s.split(newRanges, NUM_TASKS)).orElse(newRanges).forEach(r -> pendingRanges.put(r, Pair.<Token, Long>create(null, 0L)));
    // Submit a new view build task for each building range.
    // We keep record of all the submitted tasks to be able of stopping them.
    List<Future<Long>> futures = pendingRanges.entrySet().stream().map(e -> new ViewBuilderTask(baseCfs, view, e.getKey(), e.getValue().left, e.getValue().right)).peek(tasks::add).map(CompactionManager.instance::submitViewBuilder).collect(toList());
    // Add a callback to process any eventual new local range and mark the view as built, doing a delayed retry if
    // the tasks don't succeed
    Future<List<Long>> future = FutureCombiner.allOf(futures);
    future.addCallback(new FutureCallback<List<Long>>() {

        public void onSuccess(List<Long> result) {
            keysBuilt += result.stream().mapToLong(x -> x).sum();
            builtRanges.addAll(pendingRanges.keySet());
            pendingRanges.clear();
            build();
        }

        public void onFailure(Throwable t) {
            if (t instanceof CompactionInterruptedException) {
                internalStop(true);
                keysBuilt = tasks.stream().mapToLong(ViewBuilderTask::keysBuilt).sum();
                logger.info("Interrupted build for view({}.{}) after covering {} keys", ksName, view.name, keysBuilt);
            } else {
                ScheduledExecutors.nonPeriodicTasks.schedule(() -> loadStatusAndBuild(), 5, TimeUnit.MINUTES);
                logger.warn("Materialized View failed to complete, sleeping 5 minutes before restarting", t);
            }
        }
    });
    this.future = future;
}
Also used : ScheduledExecutors(org.apache.cassandra.concurrent.ScheduledExecutors) CompactionManager(org.apache.cassandra.db.compaction.CompactionManager) LoggerFactory(org.slf4j.LoggerFactory) Range(org.apache.cassandra.dht.Range) CompactionInterruptedException(org.apache.cassandra.db.compaction.CompactionInterruptedException) SystemKeyspace(org.apache.cassandra.db.SystemKeyspace) SystemDistributedKeyspace(org.apache.cassandra.schema.SystemDistributedKeyspace) Token(org.apache.cassandra.dht.Token) Replicas(org.apache.cassandra.locator.Replicas) Pair(org.apache.cassandra.utils.Pair) Map(java.util.Map) DatabaseDescriptor(org.apache.cassandra.config.DatabaseDescriptor) FutureCombiner(org.apache.cassandra.utils.concurrent.FutureCombiner) Logger(org.slf4j.Logger) FBUtilities(org.apache.cassandra.utils.FBUtilities) Set(java.util.Set) StorageService(org.apache.cassandra.service.StorageService) UUID(java.util.UUID) Collectors(java.util.stream.Collectors) Maps(com.google.common.collect.Maps) Sets(com.google.common.collect.Sets) FutureCallback(com.google.common.util.concurrent.FutureCallback) RangesAtEndpoint(org.apache.cassandra.locator.RangesAtEndpoint) TimeUnit(java.util.concurrent.TimeUnit) List(java.util.List) Collectors.toList(java.util.stream.Collectors.toList) ColumnFamilyStore(org.apache.cassandra.db.ColumnFamilyStore) Future(org.apache.cassandra.utils.concurrent.Future) ImmediateFuture(org.apache.cassandra.utils.concurrent.ImmediateFuture) RangesAtEndpoint(org.apache.cassandra.locator.RangesAtEndpoint) CompactionInterruptedException(org.apache.cassandra.db.compaction.CompactionInterruptedException) Token(org.apache.cassandra.dht.Token) Range(org.apache.cassandra.dht.Range) CompactionManager(org.apache.cassandra.db.compaction.CompactionManager) Future(org.apache.cassandra.utils.concurrent.Future) ImmediateFuture(org.apache.cassandra.utils.concurrent.ImmediateFuture) List(java.util.List) Collectors.toList(java.util.stream.Collectors.toList)

Aggregations

FutureCallback (com.google.common.util.concurrent.FutureCallback)98 ListenableFuture (com.google.common.util.concurrent.ListenableFuture)56 List (java.util.List)48 Futures (com.google.common.util.concurrent.Futures)43 ArrayList (java.util.ArrayList)41 Nullable (javax.annotation.Nullable)38 MoreExecutors (com.google.common.util.concurrent.MoreExecutors)26 Map (java.util.Map)25 Set (java.util.Set)25 TimeUnit (java.util.concurrent.TimeUnit)24 IOException (java.io.IOException)23 Collectors (java.util.stream.Collectors)23 PostConstruct (javax.annotation.PostConstruct)23 ExecutorService (java.util.concurrent.ExecutorService)22 Function (com.google.common.base.Function)21 Collections (java.util.Collections)21 TenantId (org.thingsboard.server.common.data.id.TenantId)21 PreDestroy (javax.annotation.PreDestroy)20 Logger (org.slf4j.Logger)19 LoggerFactory (org.slf4j.LoggerFactory)19