use of org.elasticsearch.hadoop.EsHadoopIllegalStateException in project elasticsearch-hadoop by elastic.
the class RestClientTest method testMainInfoWithClusterTooOld.
@Test
public void testMainInfoWithClusterTooOld() {
String response = "{\n" + "\"name\": \"node\",\n" + "\"cluster_name\": \"cluster\",\n" + "\"version\": {\n" + " \"number\": \"2.0.0\"\n" + "},\n" + "\"tagline\": \"You Know, for Search\"\n" + "}";
NetworkClient mock = Mockito.mock(NetworkClient.class);
Mockito.when(mock.execute(Mockito.any(SimpleRequest.class), Mockito.eq(true))).thenReturn(new SimpleResponse(201, new FastByteArrayInputStream(new BytesArray(response)), "localhost:9200"));
RestClient client = new RestClient(new TestSettings(), mock);
try {
client.mainInfo();
fail("Shouldn't operate on main version that is too old.");
} catch (EsHadoopIllegalStateException e) {
assertEquals("Invalid major version [2.0.0]. Version is lower than minimum required version [6.x].", e.getMessage());
}
}
use of org.elasticsearch.hadoop.EsHadoopIllegalStateException in project elasticsearch-hadoop by elastic.
the class ObjectUtils method instantiate.
@SuppressWarnings("unchecked")
public static <T> T instantiate(String className, ClassLoader loader) {
Assert.hasText(className, "No class name given");
ClassLoader cl = (loader != null ? loader : ObjectUtils.class.getClassLoader());
Class<?> clz = null;
try {
clz = cl.loadClass(className);
} catch (ClassNotFoundException e) {
throw new EsHadoopIllegalStateException(String.format("Cannot load class [%s]", className), e);
}
try {
return (T) clz.newInstance();
} catch (Exception ex) {
throw new EsHadoopIllegalStateException(String.format("Cannot instantiate class [%s]", className), ex);
}
}
use of org.elasticsearch.hadoop.EsHadoopIllegalStateException in project elasticsearch-hadoop by elastic.
the class BulkEntryWriterTest method testWriteBulkEntryWithHandlersThatCorrectsData.
@Test
public void testWriteBulkEntryWithHandlersThatCorrectsData() {
BytesRef response = new BytesRef();
response.add("abcdefg".getBytes());
BulkCommand command = Mockito.mock(BulkCommand.class);
Mockito.when(command.write(1)).thenThrow(new EsHadoopIllegalStateException("Things broke"));
Mockito.when(command.write(10)).thenReturn(response);
Settings settings = new TestSettings();
settings.setProperty(SerializationHandlerLoader.ES_WRITE_DATA_ERROR_HANDLERS, "fix");
settings.setProperty(SerializationHandlerLoader.ES_WRITE_DATA_ERROR_HANDLER + ".fix", CorrectingHandler.class.getName());
BulkEntryWriter bulkEntryWriter = new BulkEntryWriter(settings, command);
BytesRef value = bulkEntryWriter.writeBulkEntry(1);
Assert.assertNotNull("Skipped values should be null", value);
Assert.assertEquals(7, response.length());
Assert.assertArrayEquals("abcdefg".getBytes(), response.toString().getBytes());
}
use of org.elasticsearch.hadoop.EsHadoopIllegalStateException in project elasticsearch-hadoop by elastic.
the class BulkEntryWriterTest method testWriteBulkEntryWithHandlersThatPassMessages.
@Test
public void testWriteBulkEntryWithHandlersThatPassMessages() {
BulkCommand command = Mockito.mock(BulkCommand.class);
Mockito.when(command.write(1)).thenThrow(new EsHadoopIllegalStateException("Things broke"));
Settings settings = new TestSettings();
settings.setProperty(SerializationHandlerLoader.ES_WRITE_DATA_ERROR_HANDLERS, "marco,polo,skip");
settings.setProperty(SerializationHandlerLoader.ES_WRITE_DATA_ERROR_HANDLER + ".marco", MarcoHandler.class.getName());
settings.setProperty(SerializationHandlerLoader.ES_WRITE_DATA_ERROR_HANDLER + ".polo", PoloHandler.class.getName());
settings.setProperty(SerializationHandlerLoader.ES_WRITE_DATA_ERROR_HANDLER + ".skip", NothingToSeeHereHandler.class.getName());
BulkEntryWriter bulkEntryWriter = new BulkEntryWriter(settings, command);
BytesRef value = bulkEntryWriter.writeBulkEntry(1);
Assert.assertNull("Skipped values should be null", value);
}
use of org.elasticsearch.hadoop.EsHadoopIllegalStateException in project elasticsearch-hadoop by elastic.
the class RestClient method mainInfo.
public ClusterInfo mainInfo() {
Response response = execute(GET, "", true);
Map<String, Object> result = parseContent(response.body(), null);
if (result == null) {
throw new EsHadoopIllegalStateException("Unable to retrieve elasticsearch main cluster info.");
}
String clusterName = result.get("cluster_name").toString();
String clusterUUID = (String) result.get("cluster_uuid");
@SuppressWarnings("unchecked") Map<String, String> versionBody = (Map<String, String>) result.get("version");
if (versionBody == null || !StringUtils.hasText(versionBody.get("number"))) {
throw new EsHadoopIllegalStateException("Unable to retrieve elasticsearch version.");
}
String versionNumber = versionBody.get("number");
EsMajorVersion major = EsMajorVersion.parse(versionNumber);
if (major.before(EsMajorVersion.V_6_X)) {
throw new EsHadoopIllegalStateException("Invalid major version [" + major + "]. " + "Version is lower than minimum required version [" + EsMajorVersion.V_6_X + "].");
} else if (major.onOrAfter(V_6_X)) {
String tagline = result.get("tagline").toString();
if (ELASTICSEARCH_TAGLINE.equals(tagline) == false) {
LOG.warn("Could not verify server is Elasticsearch! Invalid main action response body format [tag].");
}
if (major.onOrAfter(V_7_X)) {
String buildFlavor = versionBody.get("build_flavor");
if (ELASTICSEARCH_BUILD_FLAVOR.equals(buildFlavor) == false) {
LOG.warn("Could not verify server is Elasticsearch! Invalid main action response body format [build_flavor].");
}
List<String> productHeader = response.getHeaders(ELASTIC_PRODUCT_HEADER);
boolean validElasticsearchHeader = productHeader != null && productHeader.size() == 1 && productHeader.get(0).equals(ELASTIC_PRODUCT_HEADER_VALUE);
boolean verifyServer = (major.on(V_7_X) && major.parseMinorVersion(versionNumber) >= 14) || major.onOrAfter(V_8_X);
if (validElasticsearchHeader == false) {
if (verifyServer) {
throw new EsHadoopTransportException("Connected, but could not verify server is Elasticsearch. Response missing [" + ELASTIC_PRODUCT_HEADER + "] header. Please check that you are connecting to an Elasticsearch instance, and " + "that any networking filters are preserving that header.");
} else {
LOG.warn("Could not verify server is Elasticsearch! ES-Hadoop will require server validation when connecting to an " + "Elasticsearch cluster if that Elasticsearch cluster is v7.14 and up.");
}
}
}
}
return new ClusterInfo(new ClusterName(clusterName, clusterUUID), EsMajorVersion.parse(versionNumber));
}
Aggregations