-
Notifications
You must be signed in to change notification settings - Fork 14.5k
KAFKA-19441: encapsulate MetadataImage in GroupCoordinator/ShareCoordinator #20061
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: trunk
Are you sure you want to change the base?
Changes from all commits
c582944
0fefdb3
d089ed0
12aa519
12d593b
e66548e
5bd417f
adc7ce1
0d05da2
e9bd51e
621be71
27a2df9
36055d8
a2f563e
3383d79
75738af
f7be3d8
b3b6bab
36e99ba
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,67 @@ | ||
/* | ||
* Licensed to the Apache Software Foundation (ASF) under one or more | ||
* contributor license agreements. See the NOTICE file distributed with | ||
* this work for additional information regarding copyright ownership. | ||
* The ASF licenses this file to You under the Apache License, Version 2.0 | ||
* (the "License"); you may not use this file except in compliance with | ||
* the License. You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
package org.apache.kafka.coordinator.common.runtime; | ||
|
||
import org.apache.kafka.common.Uuid; | ||
|
||
import java.util.Collection; | ||
import java.util.Set; | ||
|
||
/** | ||
* Provides metadata deltas to Coordinators (GroupCoordinator, ShareCoordinator, etc) such as changed topics and deleted topics | ||
* Implementations should be immutable. | ||
*/ | ||
public interface CoordinatorMetadataDelta { | ||
|
||
CoordinatorMetadataDelta EMPTY = emptyDelta(); | ||
|
||
Collection<Uuid> createdTopicIds(); | ||
|
||
Collection<Uuid> changedTopicIds(); | ||
|
||
Set<Uuid> deletedTopicIds(); | ||
|
||
/** | ||
* Returns the previous image of the coordinator metadata. | ||
* This image is a snapshot of the metadata before the delta occurred. | ||
*/ | ||
CoordinatorMetadataImage image(); | ||
|
||
private static CoordinatorMetadataDelta emptyDelta() { | ||
return new CoordinatorMetadataDelta() { | ||
@Override | ||
public Collection<Uuid> createdTopicIds() { | ||
return Set.of(); | ||
} | ||
|
||
@Override | ||
public Collection<Uuid> changedTopicIds() { | ||
return Set.of(); | ||
} | ||
|
||
@Override | ||
public Set<Uuid> deletedTopicIds() { | ||
return Set.of(); | ||
} | ||
|
||
@Override | ||
public CoordinatorMetadataImage image() { | ||
return CoordinatorMetadataImage.EMPTY; | ||
} | ||
}; | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,99 @@ | ||
/* | ||
* Licensed to the Apache Software Foundation (ASF) under one or more | ||
* contributor license agreements. See the NOTICE file distributed with | ||
* this work for additional information regarding copyright ownership. | ||
* The ASF licenses this file to You under the Apache License, Version 2.0 | ||
* (the "License"); you may not use this file except in compliance with | ||
* the License. You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
package org.apache.kafka.coordinator.common.runtime; | ||
|
||
import org.apache.kafka.common.Uuid; | ||
|
||
import java.util.List; | ||
import java.util.Optional; | ||
import java.util.Set; | ||
|
||
/** | ||
* Provides metadata to Coordinators (GroupCoordinator, ShareCoordinator, etc) such as topics, partitions, and their configurations. | ||
* Implementations should be thread-safe and immutable. | ||
*/ | ||
public interface CoordinatorMetadataImage { | ||
static CoordinatorMetadataImage EMPTY = emptyImage(); | ||
|
||
Set<Uuid> topicIds(); | ||
|
||
Set<String> topicNames(); | ||
|
||
Optional<TopicMetadata> topicMetadata(String topicName); | ||
|
||
Optional<TopicMetadata> topicMetadata(Uuid topicId); | ||
|
||
CoordinatorMetadataDelta emptyDelta(); | ||
|
||
long version(); | ||
|
||
boolean isEmpty(); | ||
|
||
/** | ||
* Metadata about a particular topic | ||
*/ | ||
interface TopicMetadata { | ||
String name(); | ||
|
||
Uuid id(); | ||
|
||
int partitionCount(); | ||
|
||
List<String> partitionRacks(int partitionId); | ||
} | ||
|
||
private static CoordinatorMetadataImage emptyImage() { | ||
|
||
return new CoordinatorMetadataImage() { | ||
@Override | ||
public Set<Uuid> topicIds() { | ||
return Set.of(); | ||
} | ||
|
||
@Override | ||
public Set<String> topicNames() { | ||
return Set.of(); | ||
} | ||
|
||
@Override | ||
public Optional<TopicMetadata> topicMetadata(String topicName) { | ||
return Optional.empty(); | ||
} | ||
|
||
@Override | ||
public Optional<TopicMetadata> topicMetadata(Uuid topicId) { | ||
return Optional.empty(); | ||
} | ||
|
||
@Override | ||
public CoordinatorMetadataDelta emptyDelta() { | ||
return CoordinatorMetadataDelta.EMPTY; | ||
} | ||
|
||
@Override | ||
public long version() { | ||
return 0L; | ||
} | ||
|
||
@Override | ||
public boolean isEmpty() { | ||
return true; | ||
} | ||
}; | ||
} | ||
|
||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -39,8 +39,6 @@ | |
import org.apache.kafka.common.utils.Utils; | ||
import org.apache.kafka.deferred.DeferredEvent; | ||
import org.apache.kafka.deferred.DeferredEventQueue; | ||
import org.apache.kafka.image.MetadataDelta; | ||
import org.apache.kafka.image.MetadataImage; | ||
import org.apache.kafka.server.util.timer.Timer; | ||
import org.apache.kafka.server.util.timer.TimerTask; | ||
import org.apache.kafka.storage.internals.log.LogConfig; | ||
|
@@ -2009,7 +2007,7 @@ public void onHighWatermarkUpdated( | |
/** | ||
* The latest known metadata image. | ||
*/ | ||
private volatile MetadataImage metadataImage = MetadataImage.EMPTY; | ||
private volatile CoordinatorMetadataImage metadataImage = CoordinatorMetadataImage.EMPTY; | ||
|
||
/** | ||
* Constructor. | ||
|
@@ -2474,37 +2472,37 @@ public void scheduleUnloadOperation( | |
* @param delta The metadata delta. | ||
*/ | ||
public void onNewMetadataImage( | ||
MetadataImage newImage, | ||
MetadataDelta delta | ||
CoordinatorMetadataImage newImage, | ||
CoordinatorMetadataDelta delta | ||
) { | ||
throwIfNotRunning(); | ||
log.debug("Scheduling applying of a new metadata image with offset {}.", newImage.offset()); | ||
log.debug("Scheduling applying of a new metadata image with offset {}.", newImage.version()); | ||
|
||
// Update global image. | ||
metadataImage = newImage; | ||
|
||
// Push an event for each coordinator. | ||
coordinators.keySet().forEach(tp -> { | ||
scheduleInternalOperation("UpdateImage(tp=" + tp + ", offset=" + newImage.offset() + ")", tp, () -> { | ||
scheduleInternalOperation("UpdateImage(tp=" + tp + ", offset=" + newImage.version() + ")", tp, () -> { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Should we change the There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I will update |
||
CoordinatorContext context = coordinators.get(tp); | ||
if (context != null) { | ||
context.lock.lock(); | ||
try { | ||
if (context.state == CoordinatorState.ACTIVE) { | ||
// The new image can be applied to the coordinator only if the coordinator | ||
// exists and is in the active state. | ||
log.debug("Applying new metadata image with offset {} to {}.", newImage.offset(), tp); | ||
log.debug("Applying new metadata image with offset {} to {}.", newImage.version(), tp); | ||
context.coordinator.onNewMetadataImage(newImage, delta); | ||
} else { | ||
log.debug("Ignored new metadata image with offset {} for {} because the coordinator is not active.", | ||
newImage.offset(), tp); | ||
newImage.version(), tp); | ||
} | ||
} finally { | ||
context.lock.unlock(); | ||
} | ||
} else { | ||
log.debug("Ignored new metadata image with offset {} for {} because the coordinator does not exist.", | ||
newImage.offset(), tp); | ||
newImage.version(), tp); | ||
} | ||
}); | ||
}); | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,82 @@ | ||
/* | ||
* Licensed to the Apache Software Foundation (ASF) under one or more | ||
* contributor license agreements. See the NOTICE file distributed with | ||
* this work for additional information regarding copyright ownership. | ||
* The ASF licenses this file to You under the Apache License, Version 2.0 | ||
* (the "License"); you may not use this file except in compliance with | ||
* the License. You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
package org.apache.kafka.coordinator.common.runtime; | ||
|
||
import org.apache.kafka.common.Uuid; | ||
import org.apache.kafka.image.MetadataDelta; | ||
|
||
import java.util.Collection; | ||
import java.util.Set; | ||
|
||
/** | ||
* An implementation of {@link CoordinatorMetadataDelta} that wraps the KRaft MetadataDelta. | ||
*/ | ||
public class KRaftCoordinatorMetadataDelta implements CoordinatorMetadataDelta { | ||
|
||
final MetadataDelta metadataDelta; | ||
|
||
public KRaftCoordinatorMetadataDelta(MetadataDelta metadataDelta) { | ||
this.metadataDelta = metadataDelta; | ||
} | ||
|
||
@Override | ||
public Collection<Uuid> createdTopicIds() { | ||
if (metadataDelta == null || metadataDelta.topicsDelta() == null) { | ||
return Set.of(); | ||
} | ||
return metadataDelta.topicsDelta().createdTopicIds(); | ||
} | ||
|
||
@Override | ||
public Collection<Uuid> changedTopicIds() { | ||
if (metadataDelta == null || metadataDelta.topicsDelta() == null) { | ||
return Set.of(); | ||
} | ||
return metadataDelta.topicsDelta().changedTopics().keySet(); | ||
} | ||
|
||
@Override | ||
public Set<Uuid> deletedTopicIds() { | ||
if (metadataDelta == null || metadataDelta.topicsDelta() == null) { | ||
return Set.of(); | ||
} | ||
return metadataDelta.topicsDelta().deletedTopicIds(); | ||
} | ||
|
||
|
||
@Override | ||
public String toString() { | ||
return metadataDelta.toString(); | ||
} | ||
|
||
@Override | ||
public boolean equals(Object o) { | ||
if (o == null || !o.getClass().equals(this.getClass())) return false; | ||
KRaftCoordinatorMetadataDelta other = (KRaftCoordinatorMetadataDelta) o; | ||
return metadataDelta.equals(other.metadataDelta); | ||
} | ||
|
||
@Override | ||
public int hashCode() { | ||
return metadataDelta.hashCode(); | ||
} | ||
|
||
@Override | ||
public CoordinatorMetadataImage image() { | ||
return new KRaftCoordinatorMetadataImage(metadataDelta.image()); | ||
} | ||
Comment on lines
+78
to
+81
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This method can be moved to above |
||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Could be static variable?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Actually this field is already implicitly static because it is a field defined in an interface https://docs.oracle.com/javase/specs/jls/se7/html/jls-9.html#jls-9.3