Skip to content

Commit

Permalink
fix: add missing translog sync interval option to index settings (#518)…
Browse files Browse the repository at this point in the history
… (#544)

* fix: add missing translog sync interval option to index settings

Additionally, server response contains separate `translog` object,
which cannot be deserialized by present translog option deserializers.

Therefore, this commit introduces a corresponding `Translog` class,
similarly to how it is done for the `blocks` property.



* fix: encapsulate translog options into translog in index settings

Now that all the translog options are stored in a separate object, we
should remove the separate fields to avoid confusion.

Public methods (getters, builder setters) should remain intact, mark
them deprecated and point them to the corresponding translog fields.

Deserializer should still recognize string-valued translog attributes, keep
the deprecated setters as field deserializers.

Serializer does not have to produce string-valued translog attributes,
as server parses `translog` attribute. Do not write translog contents to
old `translog.xxx` fields.



* refactor: use ModelTestCase in tests for json serialization/deserialization



---------


(cherry picked from commit 1323796)

Signed-off-by: Maksim Strutovskii <strutovsky.m.a@gmail.com>
Signed-off-by: github-actions[bot] <github-actions[bot]@users.noreply.github.com>
Co-authored-by: github-actions[bot] <github-actions[bot]@users.noreply.github.com>
  • Loading branch information
1 parent a09f260 commit ccb59f5
Show file tree
Hide file tree
Showing 11 changed files with 344 additions and 210 deletions.
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ Inspired from [Keep a Changelog](https://keepachangelog.com/en/1.0.0/)
## [Unreleased]
### Added
- Add support for knn_vector field type ([#529](https://github.com/opensearch-project/opensearch-java/pull/524))
- Add translog option object and missing translog sync interval option in index settings ([#518](https://github.com/opensearch-project/opensearch-java/pull/518))

### Dependencies
- Bumps `com.github.jk1.dependency-license-report` from 2.2 to 2.4
Expand All @@ -13,6 +14,7 @@ Inspired from [Keep a Changelog](https://keepachangelog.com/en/1.0.0/)
### Changed

### Deprecated
- Deprecate translogDurability and translogFlushThresholdSize in IndexSettings in favor of a separate translog object ([#518](https://github.com/opensearch-project/opensearch-java/pull/518))

### Removed

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,20 +36,21 @@

package org.opensearch.client.opensearch.indices;

import org.opensearch.client.opensearch._types.Time;
import jakarta.json.stream.JsonGenerator;
import org.opensearch.client.json.JsonpDeserializable;
import org.opensearch.client.json.JsonpDeserializer;
import org.opensearch.client.json.JsonpMapper;
import org.opensearch.client.json.JsonpSerializable;
import org.opensearch.client.json.ObjectBuilderDeserializer;
import org.opensearch.client.json.ObjectDeserializer;
import org.opensearch.client.opensearch._types.Time;
import org.opensearch.client.util.ApiTypeHelper;
import org.opensearch.client.util.ObjectBuilder;
import org.opensearch.client.util.ObjectBuilderBase;
import jakarta.json.stream.JsonGenerator;

import javax.annotation.Nullable;
import java.util.List;
import java.util.function.Function;
import javax.annotation.Nullable;

// typedef: indices._types.IndexSettings

Expand Down Expand Up @@ -199,10 +200,7 @@ public class IndexSettings implements JsonpSerializable {
private final Integer maxSlicesPerScroll;

@Nullable
private final String translogDurability;

@Nullable
private final String translogFlushThresholdSize;
private final Translog translog;

@Nullable
private final Boolean queryStringLenient;
Expand Down Expand Up @@ -280,8 +278,7 @@ private IndexSettings(Builder builder) {
this.verifiedBeforeClose = builder.verifiedBeforeClose;
this.format = builder.format;
this.maxSlicesPerScroll = builder.maxSlicesPerScroll;
this.translogDurability = builder.translogDurability;
this.translogFlushThresholdSize = builder.translogFlushThresholdSize;
this.translog = builder.translog;
this.queryStringLenient = builder.queryStringLenient;
this.priority = builder.priority;
this.topMetricsMaxSize = builder.topMetricsMaxSize;
Expand Down Expand Up @@ -680,20 +677,32 @@ public final Integer maxSlicesPerScroll() {
return this.maxSlicesPerScroll;
}

/**
* API name: {@code translog}
*/
@Nullable
public final Translog translog() {
return this.translog;
}

/**
* API name: {@code translog.durability}
* @deprecated use {@link #translog()} instead
*/
@Deprecated
@Nullable
public final String translogDurability() {
return this.translogDurability;
return this.translog != null ? this.translog.durability() : null;
}

/**
* API name: {@code translog.flush_threshold_size}
* @deprecated use {@link #translog()} instead
*/
@Deprecated
@Nullable
public final String translogFlushThresholdSize() {
return this.translogFlushThresholdSize;
return this.translog != null ? this.translog.flushThresholdSize() : null;
}

/**
Expand Down Expand Up @@ -1016,14 +1025,9 @@ protected void serializeInternal(JsonGenerator generator, JsonpMapper mapper) {
generator.write(this.maxSlicesPerScroll);

}
if (this.translogDurability != null) {
generator.writeKey("translog.durability");
generator.write(this.translogDurability);

}
if (this.translogFlushThresholdSize != null) {
generator.writeKey("translog.flush_threshold_size");
generator.write(this.translogFlushThresholdSize);
if (this.translog != null) {
generator.writeKey("translog");
this.translog.serialize(generator, mapper);

}
if (this.queryStringLenient != null) {
Expand Down Expand Up @@ -1221,10 +1225,7 @@ public static class Builder extends ObjectBuilderBase implements ObjectBuilder<I
private Integer maxSlicesPerScroll;

@Nullable
private String translogDurability;

@Nullable
private String translogFlushThresholdSize;
private Translog translog;

@Nullable
private Boolean queryStringLenient;
Expand Down Expand Up @@ -1716,19 +1717,43 @@ public final Builder maxSlicesPerScroll(@Nullable Integer value) {
return this;
}

/**
* API name: {@code translog}
*/
public final Builder translog(@Nullable Translog value) {
this.translog = value;
return this;
}

/**
* API name: {@code translog.durability}
* @deprecated use {@link #translog()} instead
*/
@Deprecated
public final Builder translogDurability(@Nullable String value) {
this.translogDurability = value;
if (translog == null) {
translog = Translog.of(b -> b.durability(value));
} else {
translog = Translog.of(b -> b.durability(value)
.flushThresholdSize(translog.flushThresholdSize())
.syncInterval(translog.syncInterval()));
}
return this;
}

/**
* API name: {@code translog.flush_threshold_size}
* @deprecated use {@link #translog()} instead
*/
@Deprecated
public final Builder translogFlushThresholdSize(@Nullable String value) {
this.translogFlushThresholdSize = value;
if (translog == null) {
translog = Translog.of(b -> b.flushThresholdSize(value));
} else {
translog = Translog.of(b -> b.flushThresholdSize(value)
.durability(translog.durability())
.syncInterval(translog.syncInterval()));
}
return this;
}

Expand Down Expand Up @@ -1920,6 +1945,7 @@ protected static void setupIndexSettingsDeserializer(ObjectDeserializer<IndexSet
op.add(Builder::format, JsonpDeserializer.stringDeserializer(), "format", "index.format");
op.add(Builder::maxSlicesPerScroll, JsonpDeserializer.integerDeserializer(), "max_slices_per_scroll",
"index.max_slices_per_scroll");
op.add(Builder::translog, Translog._DESERIALIZER, "translog", "index.translog");
op.add(Builder::translogDurability, JsonpDeserializer.stringDeserializer(), "translog.durability",
"index.translog.durability");
op.add(Builder::translogFlushThresholdSize, JsonpDeserializer.stringDeserializer(),
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,170 @@
/*
* SPDX-License-Identifier: Apache-2.0
*
* The OpenSearch Contributors require contributions made to
* this file be licensed under the Apache-2.0 license or a
* compatible open source license.
*/

package org.opensearch.client.opensearch.indices;

import jakarta.json.stream.JsonGenerator;
import org.opensearch.client.json.JsonpDeserializable;
import org.opensearch.client.json.JsonpDeserializer;
import org.opensearch.client.json.JsonpMapper;
import org.opensearch.client.json.JsonpSerializable;
import org.opensearch.client.json.ObjectBuilderDeserializer;
import org.opensearch.client.json.ObjectDeserializer;
import org.opensearch.client.opensearch._types.Time;
import org.opensearch.client.util.ObjectBuilder;
import org.opensearch.client.util.ObjectBuilderBase;

import javax.annotation.Nullable;
import java.util.function.Function;


@JsonpDeserializable
public class Translog implements JsonpSerializable {

@Nullable
private final String durability;

@Nullable
private final String flushThresholdSize;

@Nullable
private final Time syncInterval;

private Translog(Builder builder) {

this.durability = builder.durability;
this.flushThresholdSize = builder.flushThresholdSize;
this.syncInterval = builder.syncInterval;

}

public static Translog of(Function<Builder, ObjectBuilder<Translog>> fn) {
return fn.apply(new Builder()).build();
}

/**
* API name: {@code durability}
*/
@Nullable
public final String durability() {
return this.durability;
}

/**
* API name: {@code flush_threshold_size}
*/
@Nullable
public final String flushThresholdSize() {
return this.flushThresholdSize;
}

/**
* API name: {@code sync_interval}
*/
@Nullable
public final Time syncInterval() {
return this.syncInterval;
}

/**
* Serialize this object to JSON.
*/
public void serialize(JsonGenerator generator, JsonpMapper mapper) {
generator.writeStartObject();
serializeInternal(generator, mapper);
generator.writeEnd();
}

protected void serializeInternal(JsonGenerator generator, JsonpMapper mapper) {

if (this.durability != null) {
generator.writeKey("durability");
generator.write(this.durability);

}
if (this.flushThresholdSize != null) {
generator.writeKey("flush_threshold_size");
generator.write(this.flushThresholdSize);

}
if (this.syncInterval != null) {
generator.writeKey("sync_interval");
this.syncInterval.serialize(generator, mapper);

}

}

// ---------------------------------------------------------------------------------------------

/**
* Builder for {@link Translog}.
*/
public static class Builder extends ObjectBuilderBase implements ObjectBuilder<Translog> {

@Nullable
private String durability;

@Nullable
private String flushThresholdSize;

@Nullable
private Time syncInterval;

/**
* API name: {@code durability}
*/
public final Translog.Builder durability(@Nullable String value) {
this.durability = value;
return this;
}

/**
* API name: {@code flush_threshold_size}
*/
public final Translog.Builder flushThresholdSize(@Nullable String value) {
this.flushThresholdSize = value;
return this;
}

/**
* API name: {@code sync_interval}
*/
public final Translog.Builder syncInterval(@Nullable Time value) {
this.syncInterval = value;
return this;
}

/**
* Builds a {@link Translog}.
*
* @throws NullPointerException
* if some of the required fields are null.
*/
public Translog build() {
_checkSingleUse();

return new Translog(this);
}
}

/**
* Json deserializer for {@link Translog}
*/
public static final JsonpDeserializer<Translog> _DESERIALIZER = ObjectBuilderDeserializer.lazy(Builder::new,
Translog::setupTranslogDeserializer);

protected static void setupTranslogDeserializer(ObjectDeserializer<Translog.Builder> op) {

op.add(Translog.Builder::durability, JsonpDeserializer.stringDeserializer(), "durability");
op.add(Translog.Builder::flushThresholdSize, JsonpDeserializer.stringDeserializer(), "flush_threshold_size");
op.add(Translog.Builder::syncInterval, Time._DESERIALIZER, "sync_interval");

}

}
Loading

0 comments on commit ccb59f5

Please sign in to comment.