public static List<NodeInfo> discoverNodesIfNeeded(Settings settings, Log log) { if (settings.getNodesDiscovery()) { RestClient bootstrap = new RestClient(settings); try { List<NodeInfo> discoveredNodes = bootstrap.getHttpNodes(false); if (log.isDebugEnabled()) { log.debug(String.format("Nodes discovery enabled - found %s", discoveredNodes)); } SettingsUtils.addDiscoveredNodes(settings, discoveredNodes); return discoveredNodes; } finally { bootstrap.close(); } } return null; }
public static EsMajorVersion discoverEsVersion(Settings settings, Log log) { String version = settings.getProperty(InternalConfigurationOptions.INTERNAL_ES_VERSION); if (StringUtils.hasText(version)) { if (log.isDebugEnabled()) { log.debug(String.format("Elasticsearch version [%s] already present in configuration; skipping discovery", version)); } return EsMajorVersion.parse(version); } RestClient bootstrap = new RestClient(settings); // first get ES version try { EsMajorVersion esVersion = bootstrap.remoteEsVersion(); if (log.isDebugEnabled()) { log.debug(String.format("Discovered Elasticsearch version [%s]", esVersion)); } settings.setInternalVersion(esVersion); return esVersion; } catch (EsHadoopException ex) { throw new EsHadoopIllegalArgumentException(String.format("Cannot detect ES version - " + "typically this happens if the network/Elasticsearch cluster is not accessible or when targeting " + "a WAN/Cloud instance without the proper setting '%s'", ConfigurationOptions.ES_NODES_WAN_ONLY), ex); } finally { bootstrap.close(); } }
RestClient bootstrap = new RestClient(settings); try { String message = "Client-only routing specified but no client nodes with HTTP-enabled available";
public RestRepository(Settings settings) { this.settings = settings; this.resources = new Resources(settings); // Check if we have a read resource first, and if not, THEN check the write resource // The write resource has more strict parsing rules, and if the process is only reading // with a resource that isn't good for writing, then eagerly parsing the resource as a // write resource can erroneously throw an error. Instead, we should just get the write // resource lazily as needed. Assert.isTrue(resources.getResourceRead() != null || resources.getResourceWrite() != null, "Invalid configuration - No read or write resource specified"); this.client = new RestClient(settings); }
RestClient bootstrap = new RestClient(settings); try { String message = "No data nodes with HTTP-enabled available";
RestClient bootstrap = new RestClient(settings); try { String message = "Ingest-only routing specified but no ingest nodes with HTTP-enabled available";
public static EsMajorVersion getEsVersion() { return new RestClient(new TestSettings()).remoteEsVersion(); }
public static void checkIndexStatus(Settings settings) { if (!settings.getIndexReadAllowRedStatus()) { RestClient bootstrap = new RestClient(settings); Resource readResource = new Resource(settings, true); try { if (bootstrap.indexExists(readResource.index())) { RestClient.Health status = bootstrap.getHealth(readResource.index()); if (status == RestClient.Health.RED) { throw new EsHadoopIllegalStateException("Index specified [" + readResource.index() + "] is either red or " + "includes an index that is red, and thus all requested data cannot be safely and fully loaded. " + "Bailing out..."); } } } finally { bootstrap.close(); } } }
RestClient bootstrap = new RestClient(settings); GetAliasesRequestBuilder.Response response = null; try {
RestClient client = new RestClient(testSettings, mock);
RestClient client = new RestClient(testSettings, mock);
Mockito.when(mock.execute(Mockito.eq(request))).thenReturn(new SimpleResponse(201, new FastByteArrayInputStream(new BytesArray(response)), "localhost:9200")); RestClient client = new RestClient(new TestSettings(), mock);
@Test public void testPostDocumentSuccess() throws Exception { String index = "index/type"; Settings settings = new TestSettings(); settings.setInternalVersion(EsMajorVersion.V_7_X); settings.setResourceWrite(index); Resource writeResource = new Resource(settings, false); BytesArray document = new BytesArray("{\"field\":\"value\"}"); SimpleRequest request = new SimpleRequest(Request.Method.POST, null, index, null, document); String response = "{\n" + " \"_index\": \"index\",\n" + " \"_type\": \"type\",\n" + " \"_id\": \"AbcDefGhiJklMnoPqrS_\",\n" + " \"_version\": 1,\n" + " \"result\": \"created\",\n" + " \"_shards\": {\n" + " \"total\": 2,\n" + " \"successful\": 1,\n" + " \"failed\": 0\n" + " },\n" + " \"_seq_no\": 0,\n" + " \"_primary_term\": 1\n" + "}"; NetworkClient mock = Mockito.mock(NetworkClient.class); Mockito.when(mock.execute(Mockito.eq(request))).thenReturn(new SimpleResponse(201, new FastByteArrayInputStream(new BytesArray(response)), "localhost:9200")); RestClient client = new RestClient(new TestSettings(), mock); String id = client.postDocument(writeResource, document); assertEquals("AbcDefGhiJklMnoPqrS_", id); }
@Test(expected = EsHadoopInvalidRequest.class) public void testPostDocumentWeirdness() throws Exception { String index = "index/type"; Settings settings = new TestSettings(); settings.setInternalVersion(EsMajorVersion.V_6_X); settings.setResourceWrite(index); Resource writeResource = new Resource(settings, false); BytesArray document = new BytesArray("{\"field\":\"value\"}"); SimpleRequest request = new SimpleRequest(Request.Method.POST, null, index, null, document); String response = "{\n" + " \"_index\": \"index\",\n" + " \"_type\": \"type\",\n" + " \"definitely_not_an_id\": \"AbcDefGhiJklMnoPqrS_\",\n" + // Make the ID go away " \"_version\": 1,\n" + " \"result\": \"created\",\n" + " \"_shards\": {\n" + " \"total\": 2,\n" + " \"successful\": 1,\n" + " \"failed\": 0\n" + " },\n" + " \"_seq_no\": 0,\n" + " \"_primary_term\": 1\n" + "}"; NetworkClient mock = Mockito.mock(NetworkClient.class); Mockito.when(mock.execute(Mockito.eq(request))).thenReturn(new SimpleResponse(201, new FastByteArrayInputStream(new BytesArray(response)), "localhost:9200")); RestClient client = new RestClient(new TestSettings(), mock); String id = client.postDocument(writeResource, document); assertEquals("AbcDefGhiJklMnoPqrS_", id); }
@Test public void testPostTypelessDocumentSuccess() throws Exception { String index = "index"; Settings settings = new TestSettings(); settings.setInternalVersion(EsMajorVersion.V_7_X); settings.setResourceWrite(index); Resource writeResource = new Resource(settings, false); BytesArray document = new BytesArray("{\"field\":\"value\"}"); SimpleRequest request = new SimpleRequest(Request.Method.POST, null, index + "/_doc", null, document); String response = "{\n" + " \"_index\": \"index\",\n" + " \"_type\": \"_doc\",\n" + " \"_id\": \"AbcDefGhiJklMnoPqrS_\",\n" + " \"_version\": 1,\n" + " \"result\": \"created\",\n" + " \"_shards\": {\n" + " \"total\": 2,\n" + " \"successful\": 1,\n" + " \"failed\": 0\n" + " },\n" + " \"_seq_no\": 0,\n" + " \"_primary_term\": 1\n" + "}"; NetworkClient mock = Mockito.mock(NetworkClient.class); Mockito.when(mock.execute(Mockito.eq(request))).thenReturn(new SimpleResponse(201, new FastByteArrayInputStream(new BytesArray(response)), "localhost:9200")); RestClient client = new RestClient(new TestSettings(), mock); String id = client.postDocument(writeResource, document); assertEquals("AbcDefGhiJklMnoPqrS_", id); }
@Test(expected = EsHadoopInvalidRequest.class) public void testPostTypelessDocumentWeirdness() throws Exception { String index = "index"; Settings settings = new TestSettings(); settings.setInternalVersion(EsMajorVersion.V_7_X); settings.setResourceWrite(index); Resource writeResource = new Resource(settings, false); BytesArray document = new BytesArray("{\"field\":\"value\"}"); SimpleRequest request = new SimpleRequest(Request.Method.POST, null, index + "/_doc", null, document); String response = "{\n" + " \"_index\": \"index\",\n" + " \"_type\": \"_doc\",\n" + " \"definitely_not_an_id\": \"AbcDefGhiJklMnoPqrS_\",\n" + // Make the ID go away " \"_version\": 1,\n" + " \"result\": \"created\",\n" + " \"_shards\": {\n" + " \"total\": 2,\n" + " \"successful\": 1,\n" + " \"failed\": 0\n" + " },\n" + " \"_seq_no\": 0,\n" + " \"_primary_term\": 1\n" + "}"; NetworkClient mock = Mockito.mock(NetworkClient.class); Mockito.when(mock.execute(Mockito.eq(request))).thenReturn(new SimpleResponse(201, new FastByteArrayInputStream(new BytesArray(response)), "localhost:9200")); RestClient client = new RestClient(new TestSettings(), mock); String id = client.postDocument(writeResource, document); assertEquals("AbcDefGhiJklMnoPqrS_", id); }
Mockito.when(mock.execute(Mockito.eq(request))).thenReturn(new SimpleResponse(400, new FastByteArrayInputStream(new BytesArray(response)), "localhost:9200")); RestClient client = new RestClient(new TestSettings(), mock);
Mockito.when(mock.execute(Mockito.eq(request))).thenReturn(new SimpleResponse(400, new FastByteArrayInputStream(new BytesArray(response)), "localhost:9200")); RestClient client = new RestClient(new TestSettings(), mock);
RestClient client = new RestClient(testSettings, mock);
public RestRepository(Settings settings) { this.settings = settings; this.resources = new Resources(settings); // Check if we have a read resource first, and if not, THEN check the write resource // The write resource has more strict parsing rules, and if the process is only reading // with a resource that isn't good for writing, then eagerly parsing the resource as a // write resource can erroneously throw an error. Instead, we should just get the write // resource lazily as needed. Assert.isTrue(resources.getResourceRead() != null || resources.getResourceWrite() != null, "Invalid configuration - No read or write resource specified"); this.client = new RestClient(settings); }