-
Notifications
You must be signed in to change notification settings - Fork 25k
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
Make Settings Diffable #88815
Make Settings Diffable #88815
Changes from 1 commit
d929c80
fc5e89a
57fddf1
5eeb34f
bdf2f8f
457c701
fdeebd4
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 |
---|---|---|
|
@@ -153,7 +153,7 @@ private static <K, T, M extends Map<K, T>> MapDiff<K, T, M> createDiff( | |
inserts++; | ||
} else if (entry.getValue().equals(previousValue) == false) { | ||
if (valueSerializer.supportsDiffableValues()) { | ||
diffs.add(Map.entry(entry.getKey(), valueSerializer.diff(entry.getValue(), previousValue))); | ||
diffs.add(mapEntry(entry.getKey(), valueSerializer.diff(entry.getValue(), previousValue))); | ||
} else { | ||
upserts.add(entry); | ||
} | ||
|
@@ -307,14 +307,14 @@ private MapDiff( | |
for (int i = 0; i < diffsCount; i++) { | ||
K key = keySerializer.readKey(in); | ||
Diff<T> diff = valueSerializer.readDiff(in, key); | ||
diffs.add(Map.entry(key, diff)); | ||
diffs.add(mapEntry(key, diff)); | ||
} | ||
int upsertsCount = in.readVInt(); | ||
upserts = upsertsCount == 0 ? List.of() : new ArrayList<>(upsertsCount); | ||
for (int i = 0; i < upsertsCount; i++) { | ||
K key = keySerializer.readKey(in); | ||
T newValue = valueSerializer.read(in, key); | ||
upserts.add(Map.entry(key, newValue)); | ||
upserts.add(mapEntry(key, newValue)); | ||
} | ||
this.builderCtor = builderCtor; | ||
} | ||
|
@@ -402,6 +402,25 @@ public void writeTo(StreamOutput out) throws IOException { | |
} | ||
} | ||
|
||
private static <K, T> Map.Entry<K, T> mapEntry(K key, T newValue) { | ||
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. Why the special map entry? These will be recreated by any map impls right? 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. Yea, we really didn't need |
||
return new Map.Entry<>() { | ||
@Override | ||
public K getKey() { | ||
return key; | ||
} | ||
|
||
@Override | ||
public T getValue() { | ||
return newValue; | ||
} | ||
|
||
@Override | ||
public T setValue(T value) { | ||
throw new UnsupportedOperationException(); | ||
} | ||
}; | ||
} | ||
|
||
/** | ||
* Provides read and write operations to serialize keys of map | ||
* @param <K> type of key | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -77,7 +77,6 @@ | |
import static org.elasticsearch.cluster.node.DiscoveryNodeFilters.OpType.OR; | ||
import static org.elasticsearch.cluster.node.DiscoveryNodeFilters.validateIpValue; | ||
import static org.elasticsearch.common.settings.Settings.readSettingsFromStream; | ||
import static org.elasticsearch.common.settings.Settings.writeSettingsToStream; | ||
import static org.elasticsearch.snapshots.SearchableSnapshotsSettings.SEARCHABLE_SNAPSHOT_PARTIAL_SETTING_KEY; | ||
|
||
public class IndexMetadata implements Diffable<IndexMetadata>, ToXContentFragment { | ||
|
@@ -1313,6 +1312,8 @@ public XContentBuilder toXContent(XContentBuilder builder, Params params) throws | |
return builder; | ||
} | ||
|
||
private static final Version SETTING_DIFF_VERSION = Version.V_8_4_0; | ||
|
||
private static class IndexMetadataDiff implements Diff<IndexMetadata> { | ||
|
||
private final String index; | ||
|
@@ -1323,7 +1324,12 @@ private static class IndexMetadataDiff implements Diff<IndexMetadata> { | |
private final long aliasesVersion; | ||
private final long[] primaryTerms; | ||
private final State state; | ||
|
||
// used for BwC when this instance was written by an older version node that does not diff settings yet | ||
@Nullable | ||
private final Settings settings; | ||
@Nullable | ||
private final Diff<Settings> settingsDiff; | ||
private final Diff<ImmutableOpenMap<String, MappingMetadata>> mappings; | ||
private final Diff<ImmutableOpenMap<String, AliasMetadata>> aliases; | ||
private final Diff<ImmutableOpenMap<String, DiffableStringMap>> customData; | ||
|
@@ -1341,6 +1347,7 @@ private static class IndexMetadataDiff implements Diff<IndexMetadata> { | |
routingNumShards = after.routingNumShards; | ||
state = after.state; | ||
settings = after.settings; | ||
settingsDiff = after.settings.diff(before.settings); | ||
primaryTerms = after.primaryTerms; | ||
// TODO: find a nicer way to do BwC here and just work with Diff<MappingMetadata> here and in networking | ||
mappings = DiffableUtils.diff( | ||
|
@@ -1386,7 +1393,13 @@ private static class IndexMetadataDiff implements Diff<IndexMetadata> { | |
aliasesVersion = 1; | ||
} | ||
state = State.fromId(in.readByte()); | ||
settings = Settings.readSettingsFromStream(in); | ||
if (in.getVersion().onOrAfter(SETTING_DIFF_VERSION)) { | ||
settings = null; | ||
settingsDiff = Settings.readSettingsDiffFromStream(in); | ||
} else { | ||
settings = Settings.readSettingsFromStream(in); | ||
settingsDiff = null; | ||
} | ||
primaryTerms = in.readVLongArray(); | ||
mappings = DiffableUtils.readImmutableOpenMapDiff(in, DiffableUtils.getStringKeySerializer(), MAPPING_DIFF_VALUE_READER); | ||
aliases = DiffableUtils.readImmutableOpenMapDiff(in, DiffableUtils.getStringKeySerializer(), ALIAS_METADATA_DIFF_VALUE_READER); | ||
|
@@ -1420,7 +1433,13 @@ public void writeTo(StreamOutput out) throws IOException { | |
out.writeVLong(aliasesVersion); | ||
} | ||
out.writeByte(state.id); | ||
Settings.writeSettingsToStream(settings, out); | ||
assert settings != null | ||
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. Not the nicest way to go about BwC but I couldn't see a neater solution. |
||
: "settings should always be non-null since this instance is not expected to have been read from another node"; | ||
if (out.getVersion().onOrAfter(SETTING_DIFF_VERSION)) { | ||
settingsDiff.writeTo(out); | ||
} else { | ||
settings.writeTo(out); | ||
} | ||
out.writeVLongArray(primaryTerms); | ||
mappings.writeTo(out); | ||
aliases.writeTo(out); | ||
|
@@ -1442,7 +1461,11 @@ public IndexMetadata apply(IndexMetadata part) { | |
builder.aliasesVersion(aliasesVersion); | ||
builder.setRoutingNumShards(routingNumShards); | ||
builder.state(state); | ||
builder.settings(settings); | ||
if (settingsDiff == null) { | ||
builder.settings(settings); | ||
} else { | ||
builder.settings(settingsDiff.apply(part.settings)); | ||
} | ||
builder.primaryTerms(primaryTerms); | ||
builder.mapping = mappings.apply( | ||
ImmutableOpenMap.<String, MappingMetadata>builder(1).fPut(MapperService.SINGLE_MAPPING_NAME, part.mapping).build() | ||
|
@@ -1530,7 +1553,7 @@ public void writeTo(StreamOutput out, boolean mappingsAsHash) throws IOException | |
} | ||
out.writeInt(routingNumShards); | ||
out.writeByte(state.id()); | ||
writeSettingsToStream(settings, out); | ||
settings.writeTo(out); | ||
out.writeVLongArray(primaryTerms); | ||
// TODO: adjust serialization format to using an optional writable | ||
if (mapping == null) { | ||
|
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.
Just a quick fix here to enable
null
values as we can have them for settings. The default map entries don't allow null values.