Skip to content
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

Logsdb and source only snapshots. #122199

Merged
merged 22 commits into from
Feb 13, 2025
Merged
Show file tree
Hide file tree
Changes from 4 commits
Commits
Show all changes
22 commits
Select commit Hold shift + click to select a range
624a365
Logsdb and source only snapshots.
martijnvg Feb 10, 2025
04c4bb1
suppress forbidden api usage
martijnvg Feb 10, 2025
a06803a
Merge remote-tracking branch 'es/main' into logsdb_search_only_snapshots
martijnvg Feb 10, 2025
385e821
Update docs/changelog/122199.yaml
martijnvg Feb 10, 2025
62334b3
removed unneeded annotation
martijnvg Feb 10, 2025
6730822
Now that SFM doesn't get serialized the parser needs trigger validati…
martijnvg Feb 10, 2025
cd9beed
iter tests
martijnvg Feb 11, 2025
e7c36da
Merge remote-tracking branch 'es/main' into logsdb_search_only_snapshots
martijnvg Feb 11, 2025
f3c6a9d
update mapping asserts to account for empty _source:{} in bwc cases
martijnvg Feb 11, 2025
d4777d5
disable assertion jvm arg work around
martijnvg Feb 11, 2025
43f9d5d
Revert changes to SFM's mode attribute initializer and instead disabl…
martijnvg Feb 11, 2025
f4bf08b
Merge remote-tracking branch 'es/main' into logsdb_search_only_snapshots
martijnvg Feb 11, 2025
0e3d2f7
Merge remote-tracking branch 'es/main' into logsdb_search_only_snapshots
martijnvg Feb 12, 2025
e2addaf
added comment
martijnvg Feb 12, 2025
29df0bc
hard code parameters
martijnvg Feb 12, 2025
371229a
applied test feedback
martijnvg Feb 12, 2025
d1019f8
[CI] Auto commit changes from spotless
elasticsearchmachine Feb 12, 2025
09e3988
iter
martijnvg Feb 12, 2025
44ceea7
remove redundant test code
martijnvg Feb 12, 2025
25ef316
Merge remote-tracking branch 'es/main' into logsdb_search_only_snapshots
martijnvg Feb 13, 2025
297eccd
Merge remote-tracking branch 'es/main' into logsdb_search_only_snapshots
martijnvg Feb 13, 2025
cad4a03
Merge remote-tracking branch 'es/main' into logsdb_search_only_snapshots
martijnvg Feb 13, 2025
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -192,6 +192,8 @@ public static SegmentInfos pruneUnreferencedFiles(String segmentsFileName, Direc
throw new IllegalStateException("no commit found in the directory");
}
}
// Need to figure out what the parent field is that, so that validation in IndexWriter doesn't fail
// if no parent field is configured, but FieldInfo says there is a parent field.
String parentField = null;
final IndexCommit cp = getIndexCommit(si, directory);
try (var reader = DirectoryReader.open(cp)) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -176,7 +176,6 @@ public Builder(
true,
() -> null,
(n, c, o) -> Mode.valueOf(o.toString().toUpperCase(Locale.ROOT)),
// Avoid initializing _source.mode if it doesn't need to be serialized:
m -> toType(m).enabled.explicit() ? null : toType(m).mode,
(b, n, v) -> b.field(n, v.toString().toLowerCase(Locale.ROOT)),
v -> v.toString().toLowerCase(Locale.ROOT)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,17 +10,18 @@
import org.apache.http.client.methods.HttpPut;
import org.elasticsearch.client.Request;
import org.elasticsearch.client.Response;
import org.elasticsearch.client.RestClient;
import org.elasticsearch.common.network.InetAddresses;
import org.elasticsearch.common.settings.Settings;
import org.elasticsearch.common.time.DateFormatter;
import org.elasticsearch.common.time.FormatNames;
import org.elasticsearch.common.xcontent.XContentHelper;
import org.elasticsearch.core.SuppressForbidden;
import org.elasticsearch.repositories.fs.FsRepository;
import org.elasticsearch.test.cluster.ElasticsearchCluster;
import org.elasticsearch.test.cluster.local.distribution.DistributionType;
import org.elasticsearch.test.rest.ESRestTestCase;
import org.elasticsearch.test.rest.ObjectPath;
import org.elasticsearch.xcontent.XContentType;
import org.junit.After;
import org.junit.ClassRule;
import org.junit.rules.RuleChain;
Expand All @@ -34,6 +35,8 @@
import java.util.Locale;
import java.util.Map;

import static org.elasticsearch.test.MapMatcher.assertMap;
import static org.elasticsearch.test.MapMatcher.matchesMap;
import static org.hamcrest.Matchers.containsString;
import static org.hamcrest.Matchers.empty;
import static org.hamcrest.Matchers.equalTo;
Expand Down Expand Up @@ -78,11 +81,15 @@ public class LogsdbSnapshotRestoreIT extends ESRestTestCase {
"@timestamp" : {
"type": "date"
},
"host.name": {
"type": "keyword"
"host": {
"properties": {
"name": {
"type": "keyword"
}
}
},
"pid": {
"type": "long"
"type": "integer"
},
"method": {
"type": "keyword"
Expand All @@ -104,8 +111,8 @@ public class LogsdbSnapshotRestoreIT extends ESRestTestCase {
static final String DOC_TEMPLATE = """
{
"@timestamp": "%s",
"host.name": "%s",
"pid": "%d",
"host": { "name": "%s"},
"pid": %d,
"method": "%s",
"message": "%s",
"ip_address": "%s",
Expand Down Expand Up @@ -133,15 +140,15 @@ public void testSnapshotRestore() throws Exception {
}

public void testSnapshotRestoreWithSourceOnlyRepository() throws Exception {
snapshotAndFail("synthetic", "object", true);
snapshotAndFail("object");
}

public void testSnapshotRestoreNested() throws Exception {
snapshotAndRestore("synthetic", "nested", false);
}

public void testSnapshotRestoreNestedWithSourceOnlyRepository() throws Exception {
snapshotAndFail("synthetic", "nested", true);
snapshotAndFail("nested");
}

public void testSnapshotRestoreStoredSource() throws Exception {
Expand Down Expand Up @@ -179,22 +186,22 @@ static void snapshotAndRestore(String sourceMode, String arrayType, boolean sour
}

putTemplate("my-template", LOGS_TEMPLATE.replace("{{source_mode}}", sourceMode).replace("{{array_type}}", arrayType));
String[] docs = new String[100];
for (int i = 0; i < 100; i++) {
indexDocument(
dataStreamName,
document(
Instant.now(),
randomAlphaOfLength(10),
randomNonNegativeLong(),
randomFrom("PUT", "POST", "GET"),
randomAlphaOfLength(32),
randomIp(randomBoolean()),
randomLongBetween(1_000_000L, 2_000_000L)
)
docs[i] = document(
Instant.now(),
String.format(Locale.ROOT, "host-%03d", i),
randomNonNegativeInt(),
randomFrom("PUT", "POST", "GET"),
randomAlphaOfLength(32),
randomIp(randomBoolean()),
randomLongBetween(1_000_000L, 2_000_000L)
);
indexDocument(dataStreamName, docs[i]);
}
refresh(dataStreamName);
assertDocCount(client(), dataStreamName, 100);
assertSource(dataStreamName, docs);
assertDataStream(dataStreamName, sourceMode);

String snapshotName = "my-snapshot";
Expand All @@ -205,25 +212,26 @@ static void snapshotAndRestore(String sourceMode, String arrayType, boolean sour
List<?> failures = (List<?>) snapshotItem.get("failures");
assertThat(failures, empty());
deleteDataStream(dataStreamName);
assertDocCount(client(), dataStreamName, 0);
assertDocCount(dataStreamName, 0);

restoreSnapshot(repositoryName, snapshotName, true);
assertDataStream(dataStreamName, sourceMode);
assertDocCount(client(), dataStreamName, 100);
assertDocCount(dataStreamName, 100);
assertSource(dataStreamName, docs);
}

static void snapshotAndFail(String sourceMode, String arrayType, boolean sourceOnly) throws IOException {
static void snapshotAndFail(String arrayType) throws IOException {
String dataStreamName = "logs-my-test";
String repositoryName = "my-repository";
if (sourceOnly) {
if (true) {
var repositorySettings = Settings.builder().put("delegate_type", "fs").put("location", getRepoPath()).build();
registerRepository(repositoryName, "source", true, repositorySettings);
} else {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nit: remove, redundant.

var repositorySettings = Settings.builder().put("location", getRepoPath()).build();
registerRepository(repositoryName, FsRepository.TYPE, true, repositorySettings);
}

putTemplate("my-template", LOGS_TEMPLATE.replace("{{source_mode}}", sourceMode).replace("{{array_type}}", arrayType));
putTemplate("my-template", LOGS_TEMPLATE.replace("{{source_mode}}", "synthetic").replace("{{array_type}}", arrayType));
for (int i = 0; i < 100; i++) {
indexDocument(
dataStreamName,
Expand All @@ -234,13 +242,13 @@ static void snapshotAndFail(String sourceMode, String arrayType, boolean sourceO
randomFrom("PUT", "POST", "GET"),
randomAlphaOfLength(32),
randomIp(randomBoolean()),
randomLongBetween(1_000_000L, 2_000_000L)
randomIntBetween(1_000_000, 2_000_000)
)
);
}
refresh(dataStreamName);
assertDocCount(client(), dataStreamName, 100);
assertDataStream(dataStreamName, sourceMode);
assertDataStream(dataStreamName, "synthetic");

String snapshotName = "my-snapshot";
var snapshotResponse = performSnapshot(repositoryName, dataStreamName, snapshotName, true);
Expand Down Expand Up @@ -326,17 +334,41 @@ static String getWriteBackingIndex(String dataStreamName, int backingIndex) thro
return (String) ((Map<?, ?>) backingIndices.get(backingIndex)).get("index_name");
}

public static void assertDocCount(RestClient client, String indexName, long docCount) throws IOException {
static void assertDocCount(String indexName, long docCount) throws IOException {
Request countReq = new Request("GET", "/" + indexName + "/_count");
countReq.addParameter("ignore_unavailable", "true");
ObjectPath resp = ObjectPath.createFromResponse(client.performRequest(countReq));
ObjectPath resp = ObjectPath.createFromResponse(client().performRequest(countReq));
assertEquals(
"expected " + docCount + " documents but it was a different number",
docCount,
Long.parseLong(resp.evaluate("count").toString())
);
}

static void assertSource(String indexName, String[] docs) throws IOException {
Request searchReq = new Request("GET", "/" + indexName + "/_search");
searchReq.addParameter("size", String.valueOf(docs.length));
var response = client().performRequest(searchReq);
assertOK(response);
var responseBody = entityAsMap(response);
List<?> hits = (List<?>) ((Map<?, ?>) responseBody.get("hits")).get("hits");
assertThat(hits, hasSize(docs.length));
for (Object hit : hits) {
Map<?, ?> actualSource = (Map<?, ?>) ((Map<?, ?>) hit).get("_source");
String actualHost = (String) ((Map<?, ?>) actualSource.get("host")).get("name");
Map<?, ?> expectedSource = null;
for (String doc : docs) {
expectedSource = XContentHelper.convertToMap(XContentType.JSON.xContent(), doc, false);
String expectedHost = (String) ((Map<?, ?>) expectedSource.get("host")).get("name");
if (expectedHost.equals(actualHost)) {
break;
}
}

assertMap(actualSource, matchesMap(expectedSource));
}
}

@SuppressForbidden(reason = "TemporaryFolder only has io.File methods, not nio.File")
private static String getRepoPath() {
return repoDirectory.getRoot().getPath();
Expand Down