Search in sources :

Example 6 with Collections.emptyList

use of java.util.Collections.emptyList in project kafka by apache.

the class FetcherTest method testReturnCommittedTransactions.

@Test
public void testReturnCommittedTransactions() {
    buildFetcher(OffsetResetStrategy.EARLIEST, new ByteArrayDeserializer(), new ByteArrayDeserializer(), Integer.MAX_VALUE, IsolationLevel.READ_COMMITTED);
    ByteBuffer buffer = ByteBuffer.allocate(1024);
    int currentOffset = 0;
    currentOffset += appendTransactionalRecords(buffer, 1L, currentOffset, new SimpleRecord(time.milliseconds(), "key".getBytes(), "value".getBytes()), new SimpleRecord(time.milliseconds(), "key".getBytes(), "value".getBytes()));
    commitTransaction(buffer, 1L, currentOffset);
    buffer.flip();
    MemoryRecords records = MemoryRecords.readableRecords(buffer);
    assignFromUser(singleton(tp0));
    subscriptions.seek(tp0, 0);
    // normal fetch
    assertEquals(1, fetcher.sendFetches());
    assertFalse(fetcher.hasCompletedFetches());
    client.prepareResponse(body -> {
        FetchRequest request = (FetchRequest) body;
        assertEquals(IsolationLevel.READ_COMMITTED, request.isolationLevel());
        return true;
    }, fullFetchResponseWithAbortedTransactions(records, Collections.emptyList(), Errors.NONE, 100L, 100L, 0));
    consumerClient.poll(time.timer(0));
    assertTrue(fetcher.hasCompletedFetches());
    Map<TopicPartition, List<ConsumerRecord<byte[], byte[]>>> fetchedRecords = fetchedRecords();
    assertTrue(fetchedRecords.containsKey(tp0));
    assertEquals(fetchedRecords.get(tp0).size(), 2);
}
Also used : TopicPartition(org.apache.kafka.common.TopicPartition) SimpleRecord(org.apache.kafka.common.record.SimpleRecord) FetchRequest(org.apache.kafka.common.requests.FetchRequest) Collections.singletonList(java.util.Collections.singletonList) Arrays.asList(java.util.Arrays.asList) ArrayList(java.util.ArrayList) Collections.emptyList(java.util.Collections.emptyList) List(java.util.List) ByteArrayDeserializer(org.apache.kafka.common.serialization.ByteArrayDeserializer) ByteBuffer(java.nio.ByteBuffer) MemoryRecords(org.apache.kafka.common.record.MemoryRecords) Test(org.junit.jupiter.api.Test)

Example 7 with Collections.emptyList

use of java.util.Collections.emptyList in project kafka by apache.

the class AbstractStickyAssignorTest method testAddRemoveTwoConsumersTwoTopics.

@Test
public void testAddRemoveTwoConsumersTwoTopics() {
    List<String> allTopics = topics(topic1, topic2);
    Map<String, Integer> partitionsPerTopic = new HashMap<>();
    partitionsPerTopic.put(topic1, 3);
    partitionsPerTopic.put(topic2, 4);
    subscriptions.put(consumer1, new Subscription(allTopics));
    subscriptions.put(consumer2, new Subscription(allTopics));
    Map<String, List<TopicPartition>> assignment = assignor.assign(partitionsPerTopic, subscriptions);
    assertEquals(partitions(tp(topic1, 0), tp(topic1, 2), tp(topic2, 1), tp(topic2, 3)), assignment.get(consumer1));
    assertEquals(partitions(tp(topic1, 1), tp(topic2, 0), tp(topic2, 2)), assignment.get(consumer2));
    assertTrue(assignor.partitionsTransferringOwnership.isEmpty());
    verifyValidityAndBalance(subscriptions, assignment, partitionsPerTopic);
    assertTrue(isFullyBalanced(assignment));
    // add 2 consumers
    subscriptions.put(consumer1, buildSubscription(allTopics, assignment.get(consumer1)));
    subscriptions.put(consumer2, buildSubscription(allTopics, assignment.get(consumer2)));
    subscriptions.put(consumer3, buildSubscription(allTopics, Collections.emptyList()));
    subscriptions.put(consumer4, buildSubscription(allTopics, Collections.emptyList()));
    assignment = assignor.assign(partitionsPerTopic, subscriptions);
    Map<TopicPartition, String> expectedPartitionsTransferringOwnership = new HashMap<>();
    expectedPartitionsTransferringOwnership.put(tp(topic2, 1), consumer3);
    expectedPartitionsTransferringOwnership.put(tp(topic2, 3), consumer3);
    expectedPartitionsTransferringOwnership.put(tp(topic2, 2), consumer4);
    assertEquals(expectedPartitionsTransferringOwnership, assignor.partitionsTransferringOwnership);
    verifyValidityAndBalance(subscriptions, assignment, partitionsPerTopic);
    assertEquals(partitions(tp(topic1, 0), tp(topic1, 2)), assignment.get(consumer1));
    assertEquals(partitions(tp(topic1, 1), tp(topic2, 0)), assignment.get(consumer2));
    assertEquals(partitions(tp(topic2, 1), tp(topic2, 3)), assignment.get(consumer3));
    assertEquals(partitions(tp(topic2, 2)), assignment.get(consumer4));
    assertTrue(isFullyBalanced(assignment));
    // remove 2 consumers
    subscriptions.remove(consumer1);
    subscriptions.remove(consumer2);
    subscriptions.put(consumer3, buildSubscription(allTopics, assignment.get(consumer3)));
    subscriptions.put(consumer4, buildSubscription(allTopics, assignment.get(consumer4)));
    assignment = assignor.assign(partitionsPerTopic, subscriptions);
    assertEquals(partitions(tp(topic2, 1), tp(topic2, 3), tp(topic1, 0), tp(topic2, 0)), assignment.get(consumer3));
    assertEquals(partitions(tp(topic2, 2), tp(topic1, 1), tp(topic1, 2)), assignment.get(consumer4));
    assertTrue(assignor.partitionsTransferringOwnership.isEmpty());
    verifyValidityAndBalance(subscriptions, assignment, partitionsPerTopic);
    assertTrue(isFullyBalanced(assignment));
}
Also used : HashMap(java.util.HashMap) TopicPartition(org.apache.kafka.common.TopicPartition) Collections.emptyList(java.util.Collections.emptyList) ArrayList(java.util.ArrayList) List(java.util.List) Subscription(org.apache.kafka.clients.consumer.ConsumerPartitionAssignor.Subscription) Test(org.junit.jupiter.api.Test)

Example 8 with Collections.emptyList

use of java.util.Collections.emptyList in project crate by crate.

the class NodeJoinTests method setupMasterServiceAndCoordinator.

private void setupMasterServiceAndCoordinator(long term, ClusterState initialState, MasterService masterService, ThreadPool threadPool, Random random) {
    if (this.masterService != null || coordinator != null) {
        throw new IllegalStateException("method setupMasterServiceAndCoordinator can only be called once");
    }
    this.masterService = masterService;
    CapturingTransport capturingTransport = new CapturingTransport() {

        @Override
        protected void onSendRequest(long requestId, String action, TransportRequest request, DiscoveryNode destination) {
            if (action.equals(HANDSHAKE_ACTION_NAME)) {
                handleResponse(requestId, new TransportService.HandshakeResponse(destination, initialState.getClusterName(), destination.getVersion()));
            } else if (action.equals(JoinHelper.VALIDATE_JOIN_ACTION_NAME)) {
                handleResponse(requestId, new TransportResponse.Empty());
            } else {
                super.onSendRequest(requestId, action, request, destination);
            }
        }
    };
    final ClusterSettings clusterSettings = new ClusterSettings(Settings.EMPTY, ClusterSettings.BUILT_IN_CLUSTER_SETTINGS);
    TransportService transportService = capturingTransport.createTransportService(Settings.EMPTY, threadPool, x -> initialState.nodes().getLocalNode(), clusterSettings);
    coordinator = new Coordinator("test_node", Settings.EMPTY, clusterSettings, transportService, writableRegistry(), ESAllocationTestCase.createAllocationService(Settings.EMPTY), masterService, () -> new InMemoryPersistedState(term, initialState), r -> emptyList(), new NoOpClusterApplier(), Collections.emptyList(), random, (s, p, r) -> {
    });
    transportService.start();
    transportService.acceptIncomingRequests();
    transport = capturingTransport;
    coordinator.start();
    coordinator.startInitialJoin();
}
Also used : ESAllocationTestCase(org.elasticsearch.cluster.ESAllocationTestCase) HANDSHAKE_ACTION_NAME(org.elasticsearch.transport.TransportService.HANDSHAKE_ACTION_NAME) TransportRequest(org.elasticsearch.transport.TransportRequest) Random(java.util.Random) ClusterBlocks(org.elasticsearch.cluster.block.ClusterBlocks) CapturingTransport(org.elasticsearch.test.transport.CapturingTransport) Collections.singletonList(java.util.Collections.singletonList) ClusterState(org.elasticsearch.cluster.ClusterState) Settings(org.elasticsearch.common.settings.Settings) After(org.junit.After) ThreadPool(org.elasticsearch.threadpool.ThreadPool) ClusterName(org.elasticsearch.cluster.ClusterName) AfterClass(org.junit.AfterClass) CyclicBarrier(java.util.concurrent.CyclicBarrier) Transport(org.elasticsearch.transport.Transport) TestLogging(org.elasticsearch.test.junit.annotations.TestLogging) Collections.emptyList(java.util.Collections.emptyList) Set(java.util.Set) Collectors(java.util.stream.Collectors) List(java.util.List) Version(org.elasticsearch.Version) Stream(java.util.stream.Stream) Matchers.equalTo(org.hamcrest.Matchers.equalTo) Optional(java.util.Optional) DiscoveryNodeRole(org.elasticsearch.cluster.node.DiscoveryNodeRole) Matchers.containsString(org.hamcrest.Matchers.containsString) IntStream(java.util.stream.IntStream) BeforeClass(org.junit.BeforeClass) AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) RequestHandlerRegistry(org.elasticsearch.transport.RequestHandlerRegistry) ParameterizedMessage(org.apache.logging.log4j.message.ParameterizedMessage) AtomicReference(java.util.concurrent.atomic.AtomicReference) ArrayList(java.util.ArrayList) BaseFuture(org.elasticsearch.common.util.concurrent.BaseFuture) Metadata(org.elasticsearch.cluster.metadata.Metadata) DiscoveryNode(org.elasticsearch.cluster.node.DiscoveryNode) Node(org.elasticsearch.node.Node) TestTransportChannel(org.elasticsearch.transport.TestTransportChannel) VotingConfiguration(org.elasticsearch.cluster.coordination.CoordinationMetadata.VotingConfiguration) TransportResponse(org.elasticsearch.transport.TransportResponse) ESTestCase(org.elasticsearch.test.ESTestCase) TransportService(org.elasticsearch.transport.TransportService) MasterServiceTests(org.elasticsearch.cluster.service.MasterServiceTests) Collections.emptyMap(java.util.Collections.emptyMap) DiscoveryNodes(org.elasticsearch.cluster.node.DiscoveryNodes) TestThreadPool(org.elasticsearch.threadpool.TestThreadPool) FutureUtils(org.elasticsearch.common.util.concurrent.FutureUtils) MasterService(org.elasticsearch.cluster.service.MasterService) BrokenBarrierException(java.util.concurrent.BrokenBarrierException) TimeUnit(java.util.concurrent.TimeUnit) ClusterSettings(org.elasticsearch.common.settings.ClusterSettings) FakeThreadPoolMasterService(org.elasticsearch.cluster.service.FakeThreadPoolMasterService) ClusterServiceUtils(org.elasticsearch.test.ClusterServiceUtils) Collections(java.util.Collections) ActionListener(org.elasticsearch.action.ActionListener) Randomness(org.elasticsearch.common.Randomness) DiscoveryNode(org.elasticsearch.cluster.node.DiscoveryNode) TransportRequest(org.elasticsearch.transport.TransportRequest) ClusterSettings(org.elasticsearch.common.settings.ClusterSettings) CapturingTransport(org.elasticsearch.test.transport.CapturingTransport) Matchers.containsString(org.hamcrest.Matchers.containsString) TransportService(org.elasticsearch.transport.TransportService)

Example 9 with Collections.emptyList

use of java.util.Collections.emptyList in project open-kilda by telstra.

the class InMemoryPathComputer method getNPaths.

@Override
public List<Path> getNPaths(SwitchId srcSwitchId, SwitchId dstSwitchId, int count, FlowEncapsulationType flowEncapsulationType, PathComputationStrategy pathComputationStrategy, Duration maxLatency, Duration maxLatencyTier2) throws RecoverableException, UnroutableFlowException {
    final long maxLatencyNs = maxLatency != null ? maxLatency.toNanos() : 0;
    final long maxLatencyTier2Ns = maxLatencyTier2 != null ? maxLatencyTier2.toNanos() : 0;
    Flow flow = Flow.builder().flowId(// just any id, as not used.
    "").srcSwitch(Switch.builder().switchId(srcSwitchId).build()).destSwitch(Switch.builder().switchId(dstSwitchId).build()).ignoreBandwidth(false).encapsulationType(flowEncapsulationType).bandwidth(// to get ISLs with non zero available bandwidth
    1).maxLatency(maxLatencyNs).maxLatencyTier2(maxLatencyTier2Ns).build();
    AvailableNetwork availableNetwork = availableNetworkFactory.getAvailableNetwork(flow, Collections.emptyList());
    if (MAX_LATENCY.equals(pathComputationStrategy) && (flow.getMaxLatency() == null || flow.getMaxLatency() == 0)) {
        pathComputationStrategy = LATENCY;
    }
    List<List<Edge>> paths;
    switch(pathComputationStrategy) {
        case COST:
        case LATENCY:
        case COST_AND_AVAILABLE_BANDWIDTH:
            paths = pathFinder.findNPathsBetweenSwitches(availableNetwork, srcSwitchId, dstSwitchId, count, getWeightFunctionByStrategy(pathComputationStrategy));
            break;
        case MAX_LATENCY:
            paths = pathFinder.findNPathsBetweenSwitches(availableNetwork, srcSwitchId, dstSwitchId, count, getWeightFunctionByStrategy(pathComputationStrategy), maxLatencyNs, maxLatencyTier2Ns);
            break;
        default:
            throw new UnsupportedOperationException(String.format("Unsupported strategy type %s", pathComputationStrategy));
    }
    Comparator<Path> comparator;
    if (pathComputationStrategy == LATENCY || pathComputationStrategy == MAX_LATENCY) {
        comparator = Comparator.comparing(Path::getLatency).thenComparing(Comparator.comparing(Path::getMinAvailableBandwidth).reversed());
    } else {
        comparator = Comparator.comparing(Path::getMinAvailableBandwidth).reversed().thenComparing(Path::getLatency);
    }
    return paths.stream().map(edges -> convertToPath(srcSwitchId, dstSwitchId, edges)).sorted(comparator).limit(count).collect(Collectors.toList());
}
Also used : FlowPath(org.openkilda.model.FlowPath) Path(org.openkilda.pce.Path) MAX_LATENCY(org.openkilda.model.PathComputationStrategy.MAX_LATENCY) PathSegment(org.openkilda.model.PathSegment) FlowPath(org.openkilda.model.FlowPath) RecoverableException(org.openkilda.pce.exception.RecoverableException) ArrayList(java.util.ArrayList) HashSet(java.util.HashSet) UnroutableFlowException(org.openkilda.pce.exception.UnroutableFlowException) Edge(org.openkilda.pce.model.Edge) Flow(org.openkilda.model.Flow) AvailableNetworkFactory(org.openkilda.pce.AvailableNetworkFactory) Duration(java.time.Duration) PathComputerConfig(org.openkilda.pce.PathComputerConfig) LinkedList(java.util.LinkedList) PathId(org.openkilda.model.PathId) Path(org.openkilda.pce.Path) PathFinder(org.openkilda.pce.finder.PathFinder) FlowEncapsulationType(org.openkilda.model.FlowEncapsulationType) PathWeight(org.openkilda.pce.model.PathWeight) Switch(org.openkilda.model.Switch) PathComputationStrategy(org.openkilda.model.PathComputationStrategy) Collections.emptyList(java.util.Collections.emptyList) Collection(java.util.Collection) Set(java.util.Set) LATENCY(org.openkilda.model.PathComputationStrategy.LATENCY) Collectors(java.util.stream.Collectors) String.format(java.lang.String.format) WeightFunction(org.openkilda.pce.model.WeightFunction) Objects(java.util.Objects) Slf4j(lombok.extern.slf4j.Slf4j) List(java.util.List) SwitchId(org.openkilda.model.SwitchId) FindPathResult(org.openkilda.pce.model.FindPathResult) PathComputer(org.openkilda.pce.PathComputer) Optional(java.util.Optional) GetPathsResult(org.openkilda.pce.GetPathsResult) VisibleForTesting(com.google.common.annotations.VisibleForTesting) Comparator(java.util.Comparator) Collections(java.util.Collections) ArrayList(java.util.ArrayList) LinkedList(java.util.LinkedList) Collections.emptyList(java.util.Collections.emptyList) List(java.util.List) Flow(org.openkilda.model.Flow)

Example 10 with Collections.emptyList

use of java.util.Collections.emptyList in project kafka by apache.

the class ConsumerCoordinatorTest method testNormalJoinGroupLeader.

@Test
public void testNormalJoinGroupLeader() {
    final String consumerId = "leader";
    final Set<String> subscription = singleton(topic1);
    final List<TopicPartition> owned = Collections.emptyList();
    final List<TopicPartition> assigned = Arrays.asList(t1p);
    subscriptions.subscribe(singleton(topic1), rebalanceListener);
    // ensure metadata is up-to-date for leader
    client.updateMetadata(metadataResponse);
    client.prepareResponse(groupCoordinatorResponse(node, Errors.NONE));
    coordinator.ensureCoordinatorReady(time.timer(Long.MAX_VALUE));
    // normal join group
    Map<String, List<String>> memberSubscriptions = singletonMap(consumerId, singletonList(topic1));
    partitionAssignor.prepare(singletonMap(consumerId, assigned));
    client.prepareResponse(joinGroupLeaderResponse(1, consumerId, memberSubscriptions, Errors.NONE));
    client.prepareResponse(body -> {
        SyncGroupRequest sync = (SyncGroupRequest) body;
        return sync.data().memberId().equals(consumerId) && sync.data().generationId() == 1 && sync.groupAssignments().containsKey(consumerId);
    }, syncGroupResponse(assigned, Errors.NONE));
    coordinator.poll(time.timer(Long.MAX_VALUE));
    assertFalse(coordinator.rejoinNeededOrPending());
    assertEquals(toSet(assigned), subscriptions.assignedPartitions());
    assertEquals(subscription, subscriptions.metadataTopics());
    assertEquals(0, rebalanceListener.revokedCount);
    assertNull(rebalanceListener.revoked);
    assertEquals(1, rebalanceListener.assignedCount);
    assertEquals(getAdded(owned, assigned), rebalanceListener.assigned);
}
Also used : SyncGroupRequest(org.apache.kafka.common.requests.SyncGroupRequest) TopicPartition(org.apache.kafka.common.TopicPartition) Collections.singletonList(java.util.Collections.singletonList) ArrayList(java.util.ArrayList) Collections.emptyList(java.util.Collections.emptyList) List(java.util.List) Test(org.junit.jupiter.api.Test)

Aggregations

Collections.emptyList (java.util.Collections.emptyList)13 List (java.util.List)13 ArrayList (java.util.ArrayList)12 Collections.singletonList (java.util.Collections.singletonList)9 Test (org.junit.jupiter.api.Test)7 TopicPartition (org.apache.kafka.common.TopicPartition)6 Collections (java.util.Collections)5 HashMap (java.util.HashMap)5 Collectors (java.util.stream.Collectors)4 Stream (java.util.stream.Stream)4 ByteBuffer (java.nio.ByteBuffer)3 HashSet (java.util.HashSet)3 Objects (java.util.Objects)3 Optional (java.util.Optional)3 Set (java.util.Set)3 Arrays.asList (java.util.Arrays.asList)2 Collection (java.util.Collection)2 Random (java.util.Random)2 VisibleForTesting (com.google.common.annotations.VisibleForTesting)1 ImmutableList (com.google.common.collect.ImmutableList)1