mirror of
https://github.com/honeymoose/OpenSearch.git
synced 2025-03-01 16:39:11 +00:00
[ML] Reduce timeout to 5s while indexing notifications
Original commit: elastic/x-pack-elasticsearch@b144f366f4
This commit is contained in:
parent
61f7adbfc9
commit
73feee6317
@ -8,10 +8,12 @@ package org.elasticsearch.xpack.ml.notifications;
|
|||||||
import org.apache.logging.log4j.Logger;
|
import org.apache.logging.log4j.Logger;
|
||||||
import org.apache.logging.log4j.message.ParameterizedMessage;
|
import org.apache.logging.log4j.message.ParameterizedMessage;
|
||||||
import org.elasticsearch.action.ActionListener;
|
import org.elasticsearch.action.ActionListener;
|
||||||
|
import org.elasticsearch.action.index.IndexRequest;
|
||||||
import org.elasticsearch.action.index.IndexResponse;
|
import org.elasticsearch.action.index.IndexResponse;
|
||||||
import org.elasticsearch.client.Client;
|
import org.elasticsearch.client.Client;
|
||||||
import org.elasticsearch.cluster.service.ClusterService;
|
import org.elasticsearch.cluster.service.ClusterService;
|
||||||
import org.elasticsearch.common.logging.Loggers;
|
import org.elasticsearch.common.logging.Loggers;
|
||||||
|
import org.elasticsearch.common.unit.TimeValue;
|
||||||
import org.elasticsearch.common.xcontent.ToXContent;
|
import org.elasticsearch.common.xcontent.ToXContent;
|
||||||
import org.elasticsearch.common.xcontent.XContentBuilder;
|
import org.elasticsearch.common.xcontent.XContentBuilder;
|
||||||
|
|
||||||
@ -46,9 +48,10 @@ public class Auditor {
|
|||||||
}
|
}
|
||||||
|
|
||||||
private void indexDoc(String type, ToXContent toXContent) {
|
private void indexDoc(String type, ToXContent toXContent) {
|
||||||
client.prepareIndex(NOTIFICATIONS_INDEX, type)
|
IndexRequest indexRequest = new IndexRequest(NOTIFICATIONS_INDEX, type);
|
||||||
.setSource(toXContentBuilder(toXContent))
|
indexRequest.source(toXContentBuilder(toXContent));
|
||||||
.execute(new ActionListener<IndexResponse>() {
|
indexRequest.timeout(TimeValue.timeValueSeconds(5));
|
||||||
|
client.index(indexRequest, new ActionListener<IndexResponse>() {
|
||||||
@Override
|
@Override
|
||||||
public void onResponse(IndexResponse indexResponse) {
|
public void onResponse(IndexResponse indexResponse) {
|
||||||
LOGGER.trace("Successfully persisted {}", type);
|
LOGGER.trace("Successfully persisted {}", type);
|
||||||
|
@ -5,14 +5,13 @@
|
|||||||
*/
|
*/
|
||||||
package org.elasticsearch.xpack.ml.notifications;
|
package org.elasticsearch.xpack.ml.notifications;
|
||||||
|
|
||||||
import org.elasticsearch.action.ListenableActionFuture;
|
import org.elasticsearch.action.index.IndexRequest;
|
||||||
import org.elasticsearch.action.index.IndexRequestBuilder;
|
|
||||||
import org.elasticsearch.action.index.IndexResponse;
|
|
||||||
import org.elasticsearch.client.Client;
|
import org.elasticsearch.client.Client;
|
||||||
import org.elasticsearch.cluster.node.DiscoveryNode;
|
import org.elasticsearch.cluster.node.DiscoveryNode;
|
||||||
import org.elasticsearch.cluster.service.ClusterService;
|
import org.elasticsearch.cluster.service.ClusterService;
|
||||||
|
import org.elasticsearch.common.bytes.BytesReference;
|
||||||
|
import org.elasticsearch.common.unit.TimeValue;
|
||||||
import org.elasticsearch.common.xcontent.NamedXContentRegistry;
|
import org.elasticsearch.common.xcontent.NamedXContentRegistry;
|
||||||
import org.elasticsearch.common.xcontent.XContentBuilder;
|
|
||||||
import org.elasticsearch.common.xcontent.XContentFactory;
|
import org.elasticsearch.common.xcontent.XContentFactory;
|
||||||
import org.elasticsearch.common.xcontent.XContentParser;
|
import org.elasticsearch.common.xcontent.XContentParser;
|
||||||
import org.elasticsearch.test.ESTestCase;
|
import org.elasticsearch.test.ESTestCase;
|
||||||
@ -21,18 +20,16 @@ import org.mockito.ArgumentCaptor;
|
|||||||
|
|
||||||
import java.io.IOException;
|
import java.io.IOException;
|
||||||
|
|
||||||
|
import static org.mockito.Matchers.any;
|
||||||
import static org.mockito.Mockito.mock;
|
import static org.mockito.Mockito.mock;
|
||||||
|
import static org.mockito.Mockito.verify;
|
||||||
import static org.mockito.Mockito.when;
|
import static org.mockito.Mockito.when;
|
||||||
|
|
||||||
public class AuditorTests extends ESTestCase {
|
public class AuditorTests extends ESTestCase {
|
||||||
private Client client;
|
private Client client;
|
||||||
private ClusterService clusterService;
|
private ClusterService clusterService;
|
||||||
private ListenableActionFuture<IndexResponse> indexResponse;
|
private ArgumentCaptor<IndexRequest> indexRequestCaptor;
|
||||||
private ArgumentCaptor<String> indexCaptor;
|
|
||||||
private ArgumentCaptor<String> typeCaptor;
|
|
||||||
private ArgumentCaptor<XContentBuilder> jsonCaptor;
|
|
||||||
|
|
||||||
@SuppressWarnings("unchecked")
|
|
||||||
@Before
|
@Before
|
||||||
public void setUpMocks() {
|
public void setUpMocks() {
|
||||||
client = mock(Client.class);
|
client = mock(Client.class);
|
||||||
@ -41,61 +38,56 @@ public class AuditorTests extends ESTestCase {
|
|||||||
when(dNode.getName()).thenReturn("this_node_has_a_name");
|
when(dNode.getName()).thenReturn("this_node_has_a_name");
|
||||||
when(clusterService.localNode()).thenReturn(dNode);
|
when(clusterService.localNode()).thenReturn(dNode);
|
||||||
|
|
||||||
indexResponse = mock(ListenableActionFuture.class);
|
indexRequestCaptor = ArgumentCaptor.forClass(IndexRequest.class);
|
||||||
indexCaptor = ArgumentCaptor.forClass(String.class);
|
|
||||||
typeCaptor = ArgumentCaptor.forClass(String.class);
|
|
||||||
jsonCaptor = ArgumentCaptor.forClass(XContentBuilder.class);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
public void testInfo() throws IOException {
|
public void testInfo() throws IOException {
|
||||||
givenClientPersistsSuccessfully();
|
|
||||||
Auditor auditor = new Auditor(client, clusterService);
|
Auditor auditor = new Auditor(client, clusterService);
|
||||||
auditor.info("foo", "Here is my info");
|
auditor.info("foo", "Here is my info");
|
||||||
assertEquals(".ml-notifications", indexCaptor.getValue());
|
|
||||||
assertEquals("audit_message", typeCaptor.getValue());
|
verify(client).index(indexRequestCaptor.capture(), any());
|
||||||
AuditMessage auditMessage = parseAuditMessage();
|
IndexRequest indexRequest = indexRequestCaptor.getValue();
|
||||||
|
assertArrayEquals(new String[] {".ml-notifications"}, indexRequest.indices());
|
||||||
|
assertEquals("audit_message", indexRequest.type());
|
||||||
|
assertEquals(TimeValue.timeValueSeconds(5), indexRequest.timeout());
|
||||||
|
AuditMessage auditMessage = parseAuditMessage(indexRequest.source());
|
||||||
assertEquals("foo", auditMessage.getJobId());
|
assertEquals("foo", auditMessage.getJobId());
|
||||||
assertEquals("Here is my info", auditMessage.getMessage());
|
assertEquals("Here is my info", auditMessage.getMessage());
|
||||||
assertEquals(Level.INFO, auditMessage.getLevel());
|
assertEquals(Level.INFO, auditMessage.getLevel());
|
||||||
}
|
}
|
||||||
|
|
||||||
public void testWarning() throws IOException {
|
public void testWarning() throws IOException {
|
||||||
givenClientPersistsSuccessfully();
|
|
||||||
Auditor auditor = new Auditor(client, clusterService);
|
Auditor auditor = new Auditor(client, clusterService);
|
||||||
auditor.warning("bar", "Here is my warning");
|
auditor.warning("bar", "Here is my warning");
|
||||||
assertEquals(".ml-notifications", indexCaptor.getValue());
|
|
||||||
assertEquals("audit_message", typeCaptor.getValue());
|
verify(client).index(indexRequestCaptor.capture(), any());
|
||||||
AuditMessage auditMessage = parseAuditMessage();
|
IndexRequest indexRequest = indexRequestCaptor.getValue();
|
||||||
|
assertArrayEquals(new String[] {".ml-notifications"}, indexRequest.indices());
|
||||||
|
assertEquals("audit_message", indexRequest.type());
|
||||||
|
assertEquals(TimeValue.timeValueSeconds(5), indexRequest.timeout());
|
||||||
|
AuditMessage auditMessage = parseAuditMessage(indexRequest.source());
|
||||||
assertEquals("bar", auditMessage.getJobId());
|
assertEquals("bar", auditMessage.getJobId());
|
||||||
assertEquals("Here is my warning", auditMessage.getMessage());
|
assertEquals("Here is my warning", auditMessage.getMessage());
|
||||||
assertEquals(Level.WARNING, auditMessage.getLevel());
|
assertEquals(Level.WARNING, auditMessage.getLevel());
|
||||||
}
|
}
|
||||||
|
|
||||||
public void testError() throws IOException {
|
public void testError() throws IOException {
|
||||||
givenClientPersistsSuccessfully();
|
|
||||||
Auditor auditor = new Auditor(client, clusterService);
|
Auditor auditor = new Auditor(client, clusterService);
|
||||||
auditor.error("foobar", "Here is my error");
|
auditor.error("foobar", "Here is my error");
|
||||||
assertEquals(".ml-notifications", indexCaptor.getValue());
|
|
||||||
assertEquals("audit_message", typeCaptor.getValue());
|
verify(client).index(indexRequestCaptor.capture(), any());
|
||||||
AuditMessage auditMessage = parseAuditMessage();
|
IndexRequest indexRequest = indexRequestCaptor.getValue();
|
||||||
|
assertArrayEquals(new String[] {".ml-notifications"}, indexRequest.indices());
|
||||||
|
assertEquals("audit_message", indexRequest.type());
|
||||||
|
assertEquals(TimeValue.timeValueSeconds(5), indexRequest.timeout());
|
||||||
|
AuditMessage auditMessage = parseAuditMessage(indexRequest.source());
|
||||||
assertEquals("foobar", auditMessage.getJobId());
|
assertEquals("foobar", auditMessage.getJobId());
|
||||||
assertEquals("Here is my error", auditMessage.getMessage());
|
assertEquals("Here is my error", auditMessage.getMessage());
|
||||||
assertEquals(Level.ERROR, auditMessage.getLevel());
|
assertEquals(Level.ERROR, auditMessage.getLevel());
|
||||||
}
|
}
|
||||||
|
|
||||||
private void givenClientPersistsSuccessfully() {
|
private AuditMessage parseAuditMessage(BytesReference msg) throws IOException {
|
||||||
IndexRequestBuilder indexRequestBuilder = mock(IndexRequestBuilder.class);
|
XContentParser parser = XContentFactory.xContent(msg).createParser(NamedXContentRegistry.EMPTY, msg);
|
||||||
when(indexRequestBuilder.setSource(jsonCaptor.capture())).thenReturn(indexRequestBuilder);
|
|
||||||
when(indexRequestBuilder.execute()).thenReturn(indexResponse);
|
|
||||||
when(client.prepareIndex(indexCaptor.capture(), typeCaptor.capture()))
|
|
||||||
.thenReturn(indexRequestBuilder);
|
|
||||||
when(client.prepareIndex(indexCaptor.capture(), typeCaptor.capture()))
|
|
||||||
.thenReturn(indexRequestBuilder);
|
|
||||||
}
|
|
||||||
|
|
||||||
private AuditMessage parseAuditMessage() throws IOException {
|
|
||||||
String json = jsonCaptor.getValue().string();
|
|
||||||
XContentParser parser = XContentFactory.xContent(json).createParser(NamedXContentRegistry.EMPTY, json);
|
|
||||||
return AuditMessage.PARSER.apply(parser, null);
|
return AuditMessage.PARSER.apply(parser, null);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user