Search in sources :

Example 1 with Collections.emptyList

use of java.util.Collections.emptyList in project n4js by eclipse.

the class ScriptApiTracker method _computeMissingApiGetterSetter.

/**
 * Internal algorithm.
 */
private List<AccessorTuple> _computeMissingApiGetterSetter(TN4Classifier declaration, List<AccessorTuple> concreteAccessorTuples, Predicate<ProjectComparisonEntry> filterPredicate, boolean recursive) {
    Optional<ProjectComparisonAdapter> optAdapt = firstProjectComparisonAdapter(declaration.eResource());
    if (optAdapt.isPresent()) {
        ProjectComparisonAdapter projectComparisonAdapter = optAdapt.get();
        ProjectComparisonEntry compareEntry = projectComparisonAdapter.getEntryFor(EcoreUtil2.getContainerOfType(declaration, TModule.class));
        ProjectComparisonEntry typeCompare = compareEntry.getChildForElementImpl(declaration);
        if (typeCompare == null) {
            return Collections.emptyList();
        }
        Predicate<ProjectComparisonEntry> filter = (pce -> (pce.getElementAPI() instanceof TGetter) || (pce.getElementAPI() instanceof TSetter));
        filter = filter.and(pce -> pce.getElementImpl()[0] == null).and(filterPredicate);
        ArrayList<ProjectComparisonEntry> collectedPCEofGetterOrSetter = new ArrayList<>();
        Function<TN4Classifier, Consumer<? super ProjectComparisonEntry>> actionProvider = pivot -> pce -> {
            // Get or Set ??
            collectedPCEofGetterOrSetter.add(pce);
        };
        // recursive Extension will generate a stream of compareEntries.
        if (recursive)
            interfaceApiSupertypeWalker(filter, actionProvider, projectComparisonAdapter, (TN4Classifier) typeCompare.getElementAPI(), TN4Classifier.class);
        // ----
        /*-
				Cases of the Implementation: A getter or setter can
				- be given as AST (x)
				- be missing (m)
				- were not required by API (/)
				So we have 3*3=9 cases:
				get set
				(x) (x) --> all fine, pair will be transpiled
				(x) (m) --> code for getter will be transpiled, need to inject virtual setter code into existing tuple.
				(x) (/) --> all fine, getter will be transpiled
				(m) (x) --> code for setter will be transpiled, need to inject virtual getter code into existing tuple.
				(m) (m) --> need to create virtual accessor tuple (similar to missing field) with setter & getter
				(m) (/) --> need to create virtual accessor tuple with getter only
				(/) (x) --> all fine
				(/) (m) --> need to create virtual accessor tuple with setter only
				(/) (/) --> all fine nothing to be done.
			 */
        List<ProjectComparisonEntry> getSetList;
        if (recursive)
            getSetList = collectedPCEofGetterOrSetter;
        else
            getSetList = typeCompare.allChildren().filter(pce -> (pce.getElementAPI() instanceof TGetter) || (pce.getElementAPI() instanceof TSetter)).filter(filterPredicate).collect(Collectors.toList());
        HashMap<Pair<String, Boolean>, GetSetGroup> hmName2getset = new HashMap<>();
        for (ProjectComparisonEntry pce : getSetList) {
            TMember apiAsMember = ((TMember) pce.getElementAPI());
            String name = apiAsMember.getName();
            boolean staticCase = apiAsMember.isStatic();
            Pair<String, Boolean> key = Pair.of(name, staticCase);
            GetSetGroup group = hmName2getset.get(key);
            if (group == null) {
                group = new GetSetGroup(name, staticCase);
                hmName2getset.put(key, group);
            }
            if (pce.getElementAPI() instanceof TGetter) {
                // case getter:
                TGetter apiGetter = (TGetter) pce.getElementAPI();
                if (pce.getElementImpl(0) != null) {
                    // case (x) for getter-
                    group.getterIsInAST = true;
                } else {
                    // case (m) for getter-
                    group.getterIsInAST = false;
                    group.getter = new VirtualApiTGetter(name, apiGetter);
                }
            } else if (pce.getElementAPI() instanceof TSetter) {
                // case setter:
                TSetter apiSetter = (TSetter) pce.getElementAPI();
                if (pce.getElementImpl(0) != null) {
                    // case (x) for setter -
                    group.setterIsInAST = true;
                } else {
                    // case (m) for setter:
                    group.setterIsInAST = false;
                    group.setter = new VirtualApiTSetter(name, apiSetter);
                }
            }
        }
        // go over the list of known AccessorTupels and enhance them by adding virtual things.
        for (AccessorTuple conAccTupel : concreteAccessorTuples) {
            GetSetGroup getset = hmName2getset.remove(Pair.of(conAccTupel.getName(), conAccTupel.isStatic()));
            if (getset != null) {
                // some missings found:
                if (getset.hasGetter() && !getset.getterIsInAST && // could be mixed in by interface-default-impl different
                conAccTupel.getGetter() == null) // to the intended API-path c.f. GHOLD-212
                {
                    conAccTupel.setGetter(getset.getter);
                }
                if (getset.hasSetter() && !getset.setterIsInAST && // could be mixed in by interface-default-impl different
                conAccTupel.getSetter() == null) // to the intended API-path c.f. GHOLD-212
                {
                    conAccTupel.setSetter(getset.setter);
                }
            }
        }
        // remaining entries in hmName2getset need to translated into VirtualApiAccessors.
        List<AccessorTuple> ret = new ArrayList<>();
        for (GetSetGroup getset : hmName2getset.values()) {
            VirtualApiAccessorTuple vAccessTupel = new VirtualApiAccessorTuple(getset.name, getset.staticCases);
            if (getset.getter != null)
                vAccessTupel.setGetter(getset.getter);
            if (getset.setter != null)
                vAccessTupel.setSetter(getset.setter);
            ret.add(vAccessTupel);
        }
        return ret;
    }
    return emptyList();
}
Also used : TSetter(org.eclipse.n4js.ts.types.TSetter) ProjectComparisonEntry(org.eclipse.n4js.compare.ProjectComparisonEntry) Inject(com.google.inject.Inject) TClass(org.eclipse.n4js.ts.types.TClass) ParameterizedTypeRef(org.eclipse.n4js.ts.typeRefs.ParameterizedTypeRef) TSetter(org.eclipse.n4js.ts.types.TSetter) Logger(org.apache.log4j.Logger) TGetterImpl(org.eclipse.n4js.ts.types.impl.TGetterImpl) TMethodImpl(org.eclipse.n4js.ts.types.impl.TMethodImpl) Type(org.eclipse.n4js.ts.types.Type) MemberList(org.eclipse.n4js.ts.types.util.MemberList) N4InterfaceDeclaration(org.eclipse.n4js.n4JS.N4InterfaceDeclaration) TFieldImpl(org.eclipse.n4js.ts.types.impl.TFieldImpl) LinkedHashMultimap(com.google.common.collect.LinkedHashMultimap) Collections.emptyList(java.util.Collections.emptyList) Predicate(java.util.function.Predicate) TField(org.eclipse.n4js.ts.types.TField) EObject(org.eclipse.emf.ecore.EObject) PROVIDES_DEFAULT_IMPLEMENTATION(org.eclipse.n4js.AnnotationDefinition.PROVIDES_DEFAULT_IMPLEMENTATION) PROVIDES_INITIALZER(org.eclipse.n4js.AnnotationDefinition.PROVIDES_INITIALZER) TMethod(org.eclipse.n4js.ts.types.TMethod) Collectors(java.util.stream.Collectors) TGetter(org.eclipse.n4js.ts.types.TGetter) List(java.util.List) Stream(java.util.stream.Stream) TClassifier(org.eclipse.n4js.ts.types.TClassifier) Resource(org.eclipse.emf.ecore.resource.Resource) Optional(java.util.Optional) Pair(org.eclipse.xtext.xbase.lib.Pair) TypesFactory(org.eclipse.n4js.ts.types.TypesFactory) Singleton(com.google.inject.Singleton) HashMap(java.util.HashMap) TypeUtils(org.eclipse.n4js.ts.utils.TypeUtils) AccessorTuple(org.eclipse.n4js.ts.types.util.AccessorTuple) Function(java.util.function.Function) ArrayList(java.util.ArrayList) HashSet(java.util.HashSet) TModule(org.eclipse.n4js.ts.types.TModule) TN4Classifier(org.eclipse.n4js.ts.types.TN4Classifier) TInterface(org.eclipse.n4js.ts.types.TInterface) ProjectCompareHelper(org.eclipse.n4js.compare.ProjectCompareHelper) EcoreUtil2(org.eclipse.xtext.EcoreUtil2) TAnnotableElement(org.eclipse.n4js.ts.types.TAnnotableElement) MemberCollector(org.eclipse.n4js.utils.ContainerTypesHelper.MemberCollector) LinkedHashSet(java.util.LinkedHashSet) Iterator(java.util.Iterator) TMember(org.eclipse.n4js.ts.types.TMember) Script(org.eclipse.n4js.n4JS.Script) Consumer(java.util.function.Consumer) TypesPackage(org.eclipse.n4js.ts.types.TypesPackage) AdapterImpl(org.eclipse.emf.common.notify.impl.AdapterImpl) TSetterImpl(org.eclipse.n4js.ts.types.impl.TSetterImpl) Collections(java.util.Collections) HashMap(java.util.HashMap) TGetter(org.eclipse.n4js.ts.types.TGetter) ArrayList(java.util.ArrayList) Consumer(java.util.function.Consumer) AccessorTuple(org.eclipse.n4js.ts.types.util.AccessorTuple) TModule(org.eclipse.n4js.ts.types.TModule) ProjectComparisonEntry(org.eclipse.n4js.compare.ProjectComparisonEntry) Pair(org.eclipse.xtext.xbase.lib.Pair) TN4Classifier(org.eclipse.n4js.ts.types.TN4Classifier) TMember(org.eclipse.n4js.ts.types.TMember)

Example 2 with Collections.emptyList

use of java.util.Collections.emptyList in project component-runtime by Talend.

the class SuggestionServiceImpl method computeKeySuggestions.

@Override
public List<LookupElement> computeKeySuggestions(final Project project, final Module module, final String packageName, final List<String> containerElements, final String query) {
    final JavaPsiFacade javaPsiFacade = JavaPsiFacade.getInstance(project);
    final PsiPackage pkg = javaPsiFacade.findPackage(packageName);
    if (pkg == null) {
        return Collections.emptyList();
    }
    final String defaultFamily = getFamilyFromPackageInfo(pkg, module);
    return Stream.concat(of(pkg.getClasses()).flatMap(this::unwrapInnerClasses).filter(c -> AnnotationUtil.findAnnotation(c, PARTITION_MAPPER, PROCESSOR, EMITTER) != null).flatMap(clazz -> fromComponent(clazz, defaultFamily)), of(pkg.getClasses()).flatMap(this::unwrapInnerClasses).filter(c -> of(c.getAllFields()).anyMatch(f -> AnnotationUtil.findAnnotation(f, OPTION) != null)).flatMap(c -> fromConfiguration(defaultFamily, c.getName(), c))).filter(s -> containerElements.isEmpty() || !containerElements.contains(s.getKey())).filter(s -> query == null || query.isEmpty() || s.getKey().startsWith(query)).map(s -> s.newLookupElement(withPriority(s.getType()))).collect(toList());
}
Also used : JavaPsiFacade(com.intellij.psi.JavaPsiFacade) PsiPackage(com.intellij.psi.PsiPackage) PsiType(com.intellij.psi.PsiType) JavaPsiFacade(com.intellij.psi.JavaPsiFacade) FilenameIndex(com.intellij.psi.search.FilenameIndex) DISPLAY_NAME(org.talend.sdk.component.intellij.completion.properties.Suggestion.DISPLAY_NAME) CompletionParameters(com.intellij.codeInsight.completion.CompletionParameters) Collections.singletonList(java.util.Collections.singletonList) PsiClass(com.intellij.psi.PsiClass) PsiEnumConstant(com.intellij.psi.PsiEnumConstant) PsiClassReferenceType(com.intellij.psi.impl.source.PsiClassReferenceType) Project(com.intellij.openapi.project.Project) JavaRecursiveElementWalkingVisitor(com.intellij.psi.JavaRecursiveElementWalkingVisitor) ROOT(java.util.Locale.ROOT) Module(com.intellij.openapi.module.Module) PLACEHOLDER(org.talend.sdk.component.intellij.completion.properties.Suggestion.PLACEHOLDER) Suggestion(org.talend.sdk.component.intellij.completion.properties.Suggestion) LookupElement(com.intellij.codeInsight.lookup.LookupElement) Collections.emptyList(java.util.Collections.emptyList) Optional.ofNullable(java.util.Optional.ofNullable) Stream.of(java.util.stream.Stream.of) GlobalSearchScope(com.intellij.psi.search.GlobalSearchScope) PsiJavaFile(com.intellij.psi.PsiJavaFile) Objects(java.util.Objects) Collectors.toList(java.util.stream.Collectors.toList) List(java.util.List) Stream(java.util.stream.Stream) AnnotationUtil(com.intellij.codeInsight.AnnotationUtil) PsiAnnotation(com.intellij.psi.PsiAnnotation) JvmModifier(com.intellij.lang.jvm.JvmModifier) Collections(java.util.Collections) PsiAnnotationMemberValue(com.intellij.psi.PsiAnnotationMemberValue) PsiPackage(com.intellij.psi.PsiPackage)

Example 3 with Collections.emptyList

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

the class CheckpointCoordinatorRestoringTest method testJobGraphModificationsAreCheckedForInitialCheckpoint.

@Test
public void testJobGraphModificationsAreCheckedForInitialCheckpoint() throws Exception {
    final JobVertexID jobVertexID = new JobVertexID();
    ExecutionGraph graph = new CheckpointCoordinatorTestingUtils.CheckpointExecutionGraphBuilder().addJobVertex(jobVertexID, 1, 1).build();
    CompletedCheckpointStore completedCheckpointStore = new EmbeddedCompletedCheckpointStore();
    CompletedCheckpoint completedCheckpoint = new CompletedCheckpoint(graph.getJobID(), 2, System.currentTimeMillis(), System.currentTimeMillis() + 3000, Collections.emptyMap(), Collections.emptyList(), CheckpointProperties.forCheckpoint(CheckpointRetentionPolicy.NEVER_RETAIN_AFTER_TERMINATION), new TestCompletedCheckpointStorageLocation());
    completedCheckpointStore.addCheckpointAndSubsumeOldestOne(completedCheckpoint, new CheckpointsCleaner(), () -> {
    });
    BooleanValue checked = new BooleanValue(false);
    CheckpointCoordinator restoreCoordinator = new CheckpointCoordinatorBuilder().setExecutionGraph(graph).setCompletedCheckpointStore(completedCheckpointStore).setVertexFinishedStateCheckerFactory((vertices, states) -> new VertexFinishedStateChecker(vertices, states) {

        @Override
        public void validateOperatorsFinishedState() {
            checked.set(true);
        }
    }).build();
    restoreCoordinator.restoreInitialCheckpointIfPresent(new HashSet<>(graph.getAllVertices().values()));
    assertTrue("The finished states should be checked when job is restored on startup", checked.get());
}
Also used : CheckpointCoordinatorTestingUtils.generatePartitionableStateHandle(org.apache.flink.runtime.checkpoint.CheckpointCoordinatorTestingUtils.generatePartitionableStateHandle) BooleanValue(org.apache.flink.types.BooleanValue) ManuallyTriggeredScheduledExecutor(org.apache.flink.util.concurrent.ManuallyTriggeredScheduledExecutor) Arrays(java.util.Arrays) KeyGroupsStateHandle(org.apache.flink.runtime.state.KeyGroupsStateHandle) ChainedStateHandle(org.apache.flink.runtime.state.ChainedStateHandle) Tuple2(org.apache.flink.api.java.tuple.Tuple2) ArgumentMatchers.eq(org.mockito.ArgumentMatchers.eq) CheckpointCoordinatorBuilder(org.apache.flink.runtime.checkpoint.CheckpointCoordinatorTestingUtils.CheckpointCoordinatorBuilder) SharedStateRegistry(org.apache.flink.runtime.state.SharedStateRegistry) Random(java.util.Random) CheckpointCoordinatorTestingUtils.mockSubtaskState(org.apache.flink.runtime.checkpoint.CheckpointCoordinatorTestingUtils.mockSubtaskState) AcknowledgeCheckpoint(org.apache.flink.runtime.messages.checkpoint.AcknowledgeCheckpoint) Collections.singletonList(java.util.Collections.singletonList) Map(java.util.Map) TestLogger(org.apache.flink.util.TestLogger) StateHandleDummyUtil.createNewResultSubpartitionStateHandle(org.apache.flink.runtime.checkpoint.StateHandleDummyUtil.createNewResultSubpartitionStateHandle) Assert.fail(org.junit.Assert.fail) KeyGroupRange(org.apache.flink.runtime.state.KeyGroupRange) CheckpointCoordinatorConfigurationBuilder(org.apache.flink.runtime.jobgraph.tasks.CheckpointCoordinatorConfiguration.CheckpointCoordinatorConfigurationBuilder) Collections.emptyList(java.util.Collections.emptyList) KeyedStateHandle(org.apache.flink.runtime.state.KeyedStateHandle) Collection(java.util.Collection) Set(java.util.Set) Collectors(java.util.stream.Collectors) Objects(java.util.Objects) List(java.util.List) StateHandleDummyUtil.createNewInputChannelStateHandle(org.apache.flink.runtime.checkpoint.StateHandleDummyUtil.createNewInputChannelStateHandle) Stream(java.util.stream.Stream) Assert.assertFalse(org.junit.Assert.assertFalse) OperatorID(org.apache.flink.runtime.jobgraph.OperatorID) ArgumentMatchers.any(org.mockito.ArgumentMatchers.any) SavepointFormatType(org.apache.flink.core.execution.SavepointFormatType) ExecutionGraph(org.apache.flink.runtime.executiongraph.ExecutionGraph) TestCompletedCheckpointStorageLocation(org.apache.flink.runtime.state.testutils.TestCompletedCheckpointStorageLocation) HashMap(java.util.HashMap) CompletableFuture(java.util.concurrent.CompletableFuture) CheckpointCoordinatorTestingUtils.generateChainedPartitionableStateHandle(org.apache.flink.runtime.checkpoint.CheckpointCoordinatorTestingUtils.generateChainedPartitionableStateHandle) JobStatus(org.apache.flink.api.common.JobStatus) ArrayList(java.util.ArrayList) Execution(org.apache.flink.runtime.executiongraph.Execution) JobVertexID(org.apache.flink.runtime.jobgraph.JobVertexID) HashSet(java.util.HashSet) OperatorStateHandle(org.apache.flink.runtime.state.OperatorStateHandle) Iterables(org.apache.flink.shaded.guava30.com.google.common.collect.Iterables) Before(org.junit.Before) ExecutionJobVertex(org.apache.flink.runtime.executiongraph.ExecutionJobVertex) Assert.assertTrue(org.junit.Assert.assertTrue) Test(org.junit.Test) Mockito.times(org.mockito.Mockito.times) OperatorIDPair(org.apache.flink.runtime.OperatorIDPair) CheckpointCoordinatorTestingUtils.generateKeyGroupState(org.apache.flink.runtime.checkpoint.CheckpointCoordinatorTestingUtils.generateKeyGroupState) File(java.io.File) Mockito.verify(org.mockito.Mockito.verify) CheckpointCoordinatorTestingUtils.comparePartitionableState(org.apache.flink.runtime.checkpoint.CheckpointCoordinatorTestingUtils.comparePartitionableState) Executors(org.apache.flink.util.concurrent.Executors) Rule(org.junit.Rule) CommonTestUtils(org.apache.flink.runtime.testutils.CommonTestUtils) Assert(org.junit.Assert) CheckpointCoordinatorTestingUtils.compareKeyedState(org.apache.flink.runtime.checkpoint.CheckpointCoordinatorTestingUtils.compareKeyedState) SavepointRestoreSettings(org.apache.flink.runtime.jobgraph.SavepointRestoreSettings) Collections(java.util.Collections) TemporaryFolder(org.junit.rules.TemporaryFolder) Assert.assertEquals(org.junit.Assert.assertEquals) CheckpointCoordinatorTestingUtils.verifyStateRestore(org.apache.flink.runtime.checkpoint.CheckpointCoordinatorTestingUtils.verifyStateRestore) JobVertexID(org.apache.flink.runtime.jobgraph.JobVertexID) TestCompletedCheckpointStorageLocation(org.apache.flink.runtime.state.testutils.TestCompletedCheckpointStorageLocation) CheckpointCoordinatorBuilder(org.apache.flink.runtime.checkpoint.CheckpointCoordinatorTestingUtils.CheckpointCoordinatorBuilder) BooleanValue(org.apache.flink.types.BooleanValue) ExecutionGraph(org.apache.flink.runtime.executiongraph.ExecutionGraph) Test(org.junit.Test)

Example 4 with Collections.emptyList

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

the class ConsumerCoordinatorTest method testMetadataRefreshDuringRebalance.

@Test
public void testMetadataRefreshDuringRebalance() {
    final String consumerId = "leader";
    final List<TopicPartition> owned = Collections.emptyList();
    final List<TopicPartition> oldAssigned = singletonList(t1p);
    subscriptions.subscribe(Pattern.compile(".*"), rebalanceListener);
    client.updateMetadata(RequestTestUtils.metadataUpdateWith(1, singletonMap(topic1, 1)));
    coordinator.maybeUpdateSubscriptionMetadata();
    assertEquals(singleton(topic1), subscriptions.subscription());
    client.prepareResponse(groupCoordinatorResponse(node, Errors.NONE));
    coordinator.ensureCoordinatorReady(time.timer(Long.MAX_VALUE));
    Map<String, List<String>> initialSubscription = singletonMap(consumerId, singletonList(topic1));
    partitionAssignor.prepare(singletonMap(consumerId, oldAssigned));
    // the metadata will be updated in flight with a new topic added
    final List<String> updatedSubscription = Arrays.asList(topic1, topic2);
    client.prepareResponse(joinGroupLeaderResponse(1, consumerId, initialSubscription, Errors.NONE));
    client.prepareResponse(body -> {
        final Map<String, Integer> updatedPartitions = new HashMap<>();
        for (String topic : updatedSubscription) updatedPartitions.put(topic, 1);
        client.updateMetadata(RequestTestUtils.metadataUpdateWith(1, updatedPartitions));
        return true;
    }, syncGroupResponse(oldAssigned, Errors.NONE));
    coordinator.poll(time.timer(Long.MAX_VALUE));
    // rejoin will only be set in the next poll call
    assertFalse(coordinator.rejoinNeededOrPending());
    assertEquals(singleton(topic1), subscriptions.subscription());
    assertEquals(toSet(oldAssigned), subscriptions.assignedPartitions());
    // nothing to be revoked and hence no callback triggered
    assertEquals(0, rebalanceListener.revokedCount);
    assertNull(rebalanceListener.revoked);
    assertEquals(1, rebalanceListener.assignedCount);
    assertEquals(getAdded(owned, oldAssigned), rebalanceListener.assigned);
    List<TopicPartition> newAssigned = Arrays.asList(t1p, t2p);
    final Map<String, List<String>> updatedSubscriptions = singletonMap(consumerId, Arrays.asList(topic1, topic2));
    partitionAssignor.prepare(singletonMap(consumerId, newAssigned));
    // we expect to see a second rebalance with the new-found topics
    client.prepareResponse(body -> {
        JoinGroupRequest join = (JoinGroupRequest) body;
        Iterator<JoinGroupRequestData.JoinGroupRequestProtocol> protocolIterator = join.data().protocols().iterator();
        assertTrue(protocolIterator.hasNext());
        JoinGroupRequestData.JoinGroupRequestProtocol protocolMetadata = protocolIterator.next();
        ByteBuffer metadata = ByteBuffer.wrap(protocolMetadata.metadata());
        ConsumerPartitionAssignor.Subscription subscription = ConsumerProtocol.deserializeSubscription(metadata);
        metadata.rewind();
        return subscription.topics().containsAll(updatedSubscription);
    }, joinGroupLeaderResponse(2, consumerId, updatedSubscriptions, Errors.NONE));
    // update the metadata again back to topic1
    client.prepareResponse(body -> {
        client.updateMetadata(RequestTestUtils.metadataUpdateWith(1, singletonMap(topic1, 1)));
        return true;
    }, syncGroupResponse(newAssigned, Errors.NONE));
    coordinator.poll(time.timer(Long.MAX_VALUE));
    Collection<TopicPartition> revoked = getRevoked(oldAssigned, newAssigned);
    int revokedCount = revoked.isEmpty() ? 0 : 1;
    assertFalse(coordinator.rejoinNeededOrPending());
    assertEquals(toSet(updatedSubscription), subscriptions.subscription());
    assertEquals(toSet(newAssigned), subscriptions.assignedPartitions());
    assertEquals(revokedCount, rebalanceListener.revokedCount);
    assertEquals(revoked.isEmpty() ? null : revoked, rebalanceListener.revoked);
    assertEquals(2, rebalanceListener.assignedCount);
    assertEquals(getAdded(oldAssigned, newAssigned), rebalanceListener.assigned);
    // we expect to see a third rebalance with the new-found topics
    partitionAssignor.prepare(singletonMap(consumerId, oldAssigned));
    client.prepareResponse(body -> {
        JoinGroupRequest join = (JoinGroupRequest) body;
        Iterator<JoinGroupRequestData.JoinGroupRequestProtocol> protocolIterator = join.data().protocols().iterator();
        assertTrue(protocolIterator.hasNext());
        JoinGroupRequestData.JoinGroupRequestProtocol protocolMetadata = protocolIterator.next();
        ByteBuffer metadata = ByteBuffer.wrap(protocolMetadata.metadata());
        ConsumerPartitionAssignor.Subscription subscription = ConsumerProtocol.deserializeSubscription(metadata);
        metadata.rewind();
        return subscription.topics().contains(topic1);
    }, joinGroupLeaderResponse(3, consumerId, initialSubscription, Errors.NONE));
    client.prepareResponse(syncGroupResponse(oldAssigned, Errors.NONE));
    coordinator.poll(time.timer(Long.MAX_VALUE));
    revoked = getRevoked(newAssigned, oldAssigned);
    assertFalse(revoked.isEmpty());
    revokedCount += 1;
    Collection<TopicPartition> added = getAdded(newAssigned, oldAssigned);
    assertFalse(coordinator.rejoinNeededOrPending());
    assertEquals(singleton(topic1), subscriptions.subscription());
    assertEquals(toSet(oldAssigned), subscriptions.assignedPartitions());
    assertEquals(revokedCount, rebalanceListener.revokedCount);
    assertEquals(revoked.isEmpty() ? null : revoked, rebalanceListener.revoked);
    assertEquals(3, rebalanceListener.assignedCount);
    assertEquals(added, rebalanceListener.assigned);
    assertEquals(0, rebalanceListener.lostCount);
}
Also used : JoinGroupRequest(org.apache.kafka.common.requests.JoinGroupRequest) HashMap(java.util.HashMap) ByteBuffer(java.nio.ByteBuffer) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) 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) ConsumerPartitionAssignor(org.apache.kafka.clients.consumer.ConsumerPartitionAssignor) JoinGroupRequestData(org.apache.kafka.common.message.JoinGroupRequestData) Test(org.junit.jupiter.api.Test)

Example 5 with Collections.emptyList

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

the class ConsumerCoordinatorTest method testPatternJoinGroupLeader.

@Test
public void testPatternJoinGroupLeader() {
    final String consumerId = "leader";
    final List<TopicPartition> assigned = Arrays.asList(t1p, t2p);
    final List<TopicPartition> owned = Collections.emptyList();
    subscriptions.subscribe(Pattern.compile("test.*"), rebalanceListener);
    // partially update the metadata with one topic first,
    // let the leader to refresh metadata during assignment
    client.updateMetadata(RequestTestUtils.metadataUpdateWith(1, singletonMap(topic1, 1)));
    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));
    // expect client to force updating the metadata, if yes gives it both topics
    client.prepareMetadataUpdate(metadataResponse);
    coordinator.poll(time.timer(Long.MAX_VALUE));
    assertFalse(coordinator.rejoinNeededOrPending());
    assertEquals(2, subscriptions.numAssignedPartitions());
    assertEquals(2, subscriptions.metadataTopics().size());
    assertEquals(2, subscriptions.subscription().size());
    // callback not triggered at all since there's nothing to be revoked
    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