├── jzmq-1.0.0.jar ├── src ├── main │ ├── resources │ │ └── es-plugin.properties │ ├── java │ │ └── org │ │ │ └── elasticsearch │ │ │ ├── zeromq │ │ │ ├── ZMQServerTransport.java │ │ │ ├── exception │ │ │ │ ├── NoURIFoundZMQException.java │ │ │ │ ├── UnsupportedMethodZMQException.java │ │ │ │ └── ZMQTransportException.java │ │ │ ├── ZMQServerModule.java │ │ │ ├── ZMQServer.java │ │ │ ├── ZMQRestResponse.java │ │ │ ├── ZMQRestImpl.java │ │ │ ├── network │ │ │ │ └── ZMQAddressHelper.java │ │ │ ├── ZMQRestRequest.java │ │ │ ├── ZMQSocket.java │ │ │ └── impl │ │ │ │ └── ZMQQueueServerImpl.java │ │ │ └── plugin │ │ │ └── transport │ │ │ └── zeromq │ │ │ └── ZMQTransportPlugin.java │ └── assemblies │ │ └── esplugin.xml └── test │ ├── resources │ └── elasticsearch.yml │ └── java │ └── org │ └── elasticsearch │ └── zeromq │ └── test │ ├── SimpleClient.java │ └── ZMQTransportPluginTest.java ├── README.textile ├── pom.xml └── LICENSE.txt /jzmq-1.0.0.jar: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/tlrx/transport-zeromq/master/jzmq-1.0.0.jar -------------------------------------------------------------------------------- /src/main/resources/es-plugin.properties: -------------------------------------------------------------------------------- 1 | plugin=org.elasticsearch.plugin.transport.zeromq.ZMQTransportPlugin 2 | -------------------------------------------------------------------------------- /src/test/resources/elasticsearch.yml: -------------------------------------------------------------------------------- 1 | # Elasticsearch configuration for unit tests 2 | node: 3 | data: true 4 | local: true 5 | 6 | path: 7 | data: ./target/es/data 8 | 9 | index: 10 | store: 11 | type: memory 12 | 13 | # ZeroMQ Transport config 14 | zeromq.router.bind: tcp://*:9800 15 | zeromq.workers.threads: 2 16 | zeromq.workers.bind: inproc://es_zeromq_workers 17 | 18 | -------------------------------------------------------------------------------- /src/main/java/org/elasticsearch/zeromq/ZMQServerTransport.java: -------------------------------------------------------------------------------- 1 | /** 2 | * 3 | */ 4 | package org.elasticsearch.zeromq; 5 | 6 | import org.elasticsearch.common.component.LifecycleComponent; 7 | import org.elasticsearch.common.transport.BoundTransportAddress; 8 | 9 | /** 10 | * @author tlrx 11 | * 12 | */ 13 | public interface ZMQServerTransport extends LifecycleComponent { 14 | 15 | BoundTransportAddress boundAddress(); 16 | } 17 | -------------------------------------------------------------------------------- /src/main/assemblies/esplugin.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | bin 4 | 5 | zip 6 | 7 | false 8 | 9 | 10 | false 11 | / 12 | true 13 | true 14 | 15 | org.elasticsearch:elasticsearch 16 | junit:junit 17 | log4j:log4j 18 | 19 | 20 | 21 | 22 | 23 | ${project.build.directory}/ 24 | / 25 | 26 | ${project.name}-${project.version}.jar 27 | **/jzmq*.jar 28 | 29 | 30 | 31 | 32 | -------------------------------------------------------------------------------- /src/main/java/org/elasticsearch/zeromq/exception/NoURIFoundZMQException.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Licensed to Elastic Search and Shay Banon under one 3 | * or more contributor license agreements. See the NOTICE file 4 | * distributed with this work for additional information 5 | * regarding copyright ownership. Elastic Search licenses this 6 | * file to you under the Apache License, Version 2.0 (the 7 | * "License"); you may not use this file except in compliance 8 | * with the License. You may obtain a copy of the License at 9 | * 10 | * http://www.apache.org/licenses/LICENSE-2.0 11 | * 12 | * Unless required by applicable law or agreed to in writing, 13 | * software distributed under the License is distributed on an 14 | * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY 15 | * KIND, either express or implied. See the License for the 16 | * specific language governing permissions and limitations 17 | * under the License. 18 | */ 19 | 20 | package org.elasticsearch.zeromq.exception; 21 | 22 | /** 23 | * @author tlrx 24 | */ 25 | public class NoURIFoundZMQException extends ZMQTransportException { 26 | 27 | public NoURIFoundZMQException() { 28 | super("No URI found in message"); 29 | } 30 | } 31 | -------------------------------------------------------------------------------- /src/main/java/org/elasticsearch/zeromq/exception/UnsupportedMethodZMQException.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Licensed to Elastic Search and Shay Banon under one 3 | * or more contributor license agreements. See the NOTICE file 4 | * distributed with this work for additional information 5 | * regarding copyright ownership. Elastic Search licenses this 6 | * file to you under the Apache License, Version 2.0 (the 7 | * "License"); you may not use this file except in compliance 8 | * with the License. You may obtain a copy of the License at 9 | * 10 | * http://www.apache.org/licenses/LICENSE-2.0 11 | * 12 | * Unless required by applicable law or agreed to in writing, 13 | * software distributed under the License is distributed on an 14 | * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY 15 | * KIND, either express or implied. See the License for the 16 | * specific language governing permissions and limitations 17 | * under the License. 18 | */ 19 | 20 | package org.elasticsearch.zeromq.exception; 21 | 22 | /** 23 | * @author tlrx 24 | */ 25 | public class UnsupportedMethodZMQException extends ZMQTransportException { 26 | 27 | public UnsupportedMethodZMQException(String method) { 28 | super("Unsupported method " + method); 29 | } 30 | } 31 | -------------------------------------------------------------------------------- /src/main/java/org/elasticsearch/zeromq/exception/ZMQTransportException.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Licensed to Elastic Search and Shay Banon under one 3 | * or more contributor license agreements. See the NOTICE file 4 | * distributed with this work for additional information 5 | * regarding copyright ownership. Elastic Search licenses this 6 | * file to you under the Apache License, Version 2.0 (the 7 | * "License"); you may not use this file except in compliance 8 | * with the License. You may obtain a copy of the License at 9 | * 10 | * http://www.apache.org/licenses/LICENSE-2.0 11 | * 12 | * Unless required by applicable law or agreed to in writing, 13 | * software distributed under the License is distributed on an 14 | * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY 15 | * KIND, either express or implied. See the License for the 16 | * specific language governing permissions and limitations 17 | * under the License. 18 | */ 19 | 20 | package org.elasticsearch.zeromq.exception; 21 | 22 | import org.elasticsearch.transport.TransportException; 23 | 24 | /** 25 | * @author tlrx 26 | */ 27 | public class ZMQTransportException extends TransportException { 28 | 29 | public ZMQTransportException(String msg) { 30 | super(msg); 31 | } 32 | 33 | public ZMQTransportException(String msg, Throwable cause) { 34 | super(msg, cause); 35 | } 36 | } 37 | -------------------------------------------------------------------------------- /src/main/java/org/elasticsearch/zeromq/ZMQServerModule.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Licensed to Elastic Search and Shay Banon under one 3 | * or more contributor license agreements. See the NOTICE file 4 | * distributed with this work for additional information 5 | * regarding copyright ownership. Elastic Search licenses this 6 | * file to you under the Apache License, Version 2.0 (the 7 | * "License"); you may not use this file except in compliance 8 | * with the License. You may obtain a copy of the License at 9 | * 10 | * http://www.apache.org/licenses/LICENSE-2.0 11 | * 12 | * Unless required by applicable law or agreed to in writing, 13 | * software distributed under the License is distributed on an 14 | * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY 15 | * KIND, either express or implied. See the License for the 16 | * specific language governing permissions and limitations 17 | * under the License. 18 | */ 19 | 20 | package org.elasticsearch.zeromq; 21 | 22 | import org.elasticsearch.common.inject.AbstractModule; 23 | import org.elasticsearch.common.settings.Settings; 24 | import org.elasticsearch.zeromq.impl.ZMQQueueServerImpl; 25 | 26 | /** 27 | * @author tlrx 28 | */ 29 | public class ZMQServerModule extends AbstractModule { 30 | 31 | private final Settings settings; 32 | 33 | public ZMQServerModule(Settings settings) { 34 | this.settings = settings; 35 | } 36 | 37 | @Override 38 | protected void configure() { 39 | bind(ZMQRestImpl.class).asEagerSingleton(); 40 | bind(ZMQServerTransport.class).to(ZMQQueueServerImpl.class).asEagerSingleton(); 41 | bind(ZMQServer.class).asEagerSingleton(); 42 | } 43 | } 44 | -------------------------------------------------------------------------------- /src/main/java/org/elasticsearch/zeromq/ZMQServer.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Licensed to Elastic Search and Shay Banon under one 3 | * or more contributor license agreements. See the NOTICE file 4 | * distributed with this work for additional information 5 | * regarding copyright ownership. Elastic Search licenses this 6 | * file to you under the Apache License, Version 2.0 (the 7 | * "License"); you may not use this file except in compliance 8 | * with the License. You may obtain a copy of the License at 9 | * 10 | * http://www.apache.org/licenses/LICENSE-2.0 11 | * 12 | * Unless required by applicable law or agreed to in writing, 13 | * software distributed under the License is distributed on an 14 | * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY 15 | * KIND, either express or implied. See the License for the 16 | * specific language governing permissions and limitations 17 | * under the License. 18 | */ 19 | 20 | package org.elasticsearch.zeromq; 21 | 22 | import org.elasticsearch.ElasticSearchException; 23 | import org.elasticsearch.common.component.AbstractLifecycleComponent; 24 | import org.elasticsearch.common.inject.Inject; 25 | import org.elasticsearch.common.settings.Settings; 26 | import org.elasticsearch.node.service.NodeService; 27 | 28 | import static org.elasticsearch.common.util.concurrent.EsExecutors.*; 29 | 30 | /** 31 | * @author tlrx 32 | */ 33 | public class ZMQServer extends AbstractLifecycleComponent { 34 | 35 | private final NodeService nodeService; 36 | 37 | private volatile ZMQServerTransport transport; 38 | 39 | @Inject 40 | public ZMQServer(Settings settings, NodeService nodeService, 41 | ZMQServerTransport transport, ZMQRestImpl client) { 42 | 43 | super(settings); 44 | this.transport = transport; 45 | this.nodeService = nodeService; 46 | } 47 | 48 | @Override 49 | protected void doStart() throws ElasticSearchException { 50 | 51 | logger.debug("Starting ØMQ server..."); 52 | daemonThreadFactory(settings, "zeromq_server").newThread( 53 | new Runnable() { 54 | @Override 55 | public void run() { 56 | transport.start(); 57 | } 58 | }).start(); 59 | } 60 | 61 | 62 | 63 | @Override 64 | protected void doStop() throws ElasticSearchException { 65 | nodeService.removeAttribute("zeromq_address"); 66 | transport.stop(); 67 | } 68 | 69 | @Override 70 | protected void doClose() throws ElasticSearchException { 71 | transport.close(); 72 | } 73 | } 74 | -------------------------------------------------------------------------------- /src/main/java/org/elasticsearch/zeromq/ZMQRestResponse.java: -------------------------------------------------------------------------------- 1 | package org.elasticsearch.zeromq; 2 | 3 | import java.io.IOException; 4 | import java.nio.ByteBuffer; 5 | 6 | import org.elasticsearch.common.Bytes; 7 | import org.elasticsearch.rest.AbstractRestResponse; 8 | import org.elasticsearch.rest.RestStatus; 9 | 10 | /** 11 | * @author tlrx 12 | * 13 | */ 14 | public class ZMQRestResponse extends AbstractRestResponse { 15 | 16 | private final RestStatus status; 17 | 18 | public ByteBuffer body; 19 | 20 | private String contentType; 21 | 22 | public ZMQRestResponse(RestStatus status) { 23 | super(); 24 | this.status = status; 25 | } 26 | 27 | @Override 28 | public String contentType() { 29 | return contentType; 30 | } 31 | 32 | public ZMQRestResponse setBody(ByteBuffer body) { 33 | this.body = body; 34 | return this; 35 | } 36 | 37 | @Override 38 | public byte[] content() throws IOException { 39 | if (body == null) { 40 | return Bytes.EMPTY_ARRAY; 41 | } 42 | return body.array(); 43 | } 44 | 45 | @Override 46 | public int contentLength() throws IOException { 47 | if (body == null) { 48 | return 0; 49 | } 50 | return body.remaining(); 51 | } 52 | 53 | @Override 54 | public RestStatus status() { 55 | return status; 56 | } 57 | 58 | @Override 59 | public boolean contentThreadSafe() { 60 | return false; 61 | } 62 | 63 | public void setContentType(String contentType) { 64 | this.contentType = contentType; 65 | } 66 | 67 | /** 68 | * @return the payload to reply to the client 69 | * @throws IOException 70 | */ 71 | public byte[] payload() { 72 | 73 | // TODO optimise & challenge thoses lines... 74 | ByteBuffer bStatusCode = ByteBuffer.wrap(Integer.toString(this.status.getStatus()).getBytes()); 75 | ByteBuffer bStatusName = ByteBuffer.wrap(this.status.name().getBytes()); 76 | ByteBuffer bSep1 = ByteBuffer.wrap(ZMQSocket.SEPARATOR.getBytes()); 77 | ByteBuffer bSep2 = ByteBuffer.wrap(ZMQSocket.SEPARATOR.getBytes()); 78 | ByteBuffer bContent = null; 79 | 80 | try { 81 | bContent = ByteBuffer.wrap(content()); 82 | } catch (Exception e) { 83 | bContent = ByteBuffer.wrap(e.getMessage().getBytes()); 84 | } 85 | 86 | ByteBuffer payload = ByteBuffer.allocate(bStatusCode.limit() + bSep1.limit() + bStatusName.limit() + bSep2.limit() + bContent.limit()); 87 | payload.put(bStatusCode); 88 | payload.put(bSep1); 89 | payload.put(bStatusName); 90 | payload.put(bSep2); 91 | payload.put(bContent); 92 | 93 | return payload.array(); 94 | } 95 | } 96 | -------------------------------------------------------------------------------- /src/main/java/org/elasticsearch/plugin/transport/zeromq/ZMQTransportPlugin.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Licensed to Elastic Search and Shay Banon under one 3 | * or more contributor license agreements. See the NOTICE file 4 | * distributed with this work for additional information 5 | * regarding copyright ownership. Elastic Search licenses this 6 | * file to you under the Apache License, Version 2.0 (the 7 | * "License"); you may not use this file except in compliance 8 | * with the License. You may obtain a copy of the License at 9 | * 10 | * http://www.apache.org/licenses/LICENSE-2.0 11 | * 12 | * Unless required by applicable law or agreed to in writing, 13 | * software distributed under the License is distributed on an 14 | * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY 15 | * KIND, either express or implied. See the License for the 16 | * specific language governing permissions and limitations 17 | * under the License. 18 | */ 19 | 20 | package org.elasticsearch.plugin.transport.zeromq; 21 | 22 | import static org.elasticsearch.common.collect.Lists.newArrayList; 23 | 24 | import java.util.Collection; 25 | 26 | import org.elasticsearch.common.component.LifecycleComponent; 27 | import org.elasticsearch.common.inject.Module; 28 | import org.elasticsearch.common.settings.Settings; 29 | import org.elasticsearch.plugins.AbstractPlugin; 30 | import org.elasticsearch.zeromq.ZMQServer; 31 | import org.elasticsearch.zeromq.ZMQServerModule; 32 | 33 | /** 34 | * @author tlrx 35 | */ 36 | public class ZMQTransportPlugin extends AbstractPlugin { 37 | 38 | private final Settings settings; 39 | 40 | public ZMQTransportPlugin(Settings settings) { 41 | this.settings = settings; 42 | } 43 | 44 | @Override public String name() { 45 | return "transport-zeromq"; 46 | } 47 | 48 | @Override public String description() { 49 | return "Exports elasticsearch REST APIs over ØMQ"; 50 | } 51 | 52 | @Override public Collection> modules() { 53 | Collection> modules = newArrayList(); 54 | if (settings.getAsBoolean("zeromq.enabled", true)) { 55 | modules.add(ZMQServerModule.class); 56 | } 57 | return modules; 58 | } 59 | 60 | @Override public Collection> services() { 61 | Collection> services = newArrayList(); 62 | if (settings.getAsBoolean("zeromq.enabled", true)) { 63 | services.add(ZMQServer.class); 64 | } 65 | return services; 66 | } 67 | } 68 | -------------------------------------------------------------------------------- /src/test/java/org/elasticsearch/zeromq/test/SimpleClient.java: -------------------------------------------------------------------------------- 1 | /** 2 | * 3 | */ 4 | package org.elasticsearch.zeromq.test; 5 | 6 | import java.io.UnsupportedEncodingException; 7 | 8 | import org.elasticsearch.zeromq.ZMQSocket; 9 | import org.zeromq.ZMQ; 10 | 11 | /** 12 | * A simple ØMQ client (XREQ) 13 | * 14 | * @author tlrx 15 | * 16 | */ 17 | public class SimpleClient { 18 | 19 | /** 20 | * Format a message 21 | * @param method 22 | * @return 23 | * @throws UnsupportedEncodingException 24 | */ 25 | private static byte[] format(String method, String url, String json, Integer count) throws UnsupportedEncodingException { 26 | StringBuilder sb = new StringBuilder(method); 27 | sb.append(ZMQSocket.SEPARATOR); 28 | if (count != null) { 29 | sb.append(url).append(count); 30 | } else { 31 | sb.append(url); 32 | } 33 | sb.append(ZMQSocket.SEPARATOR); 34 | 35 | if (json != null) { 36 | sb.append(json); 37 | } 38 | return sb.toString().getBytes("UTF-8"); 39 | } 40 | 41 | /** 42 | * @param args 43 | */ 44 | public static void main(String[] args) { 45 | 46 | if (args == null || args.length < 3) { 47 | System.err 48 | .println("Usage: SimpleClient
"); 49 | return; 50 | } 51 | 52 | // tcp://localhost:9700 by default 53 | String address = args[0]; 54 | String method = args[1]; 55 | String url = args[2]; 56 | String json = null; 57 | Integer repeat = 1; 58 | if(args.length > 3){ 59 | json = args[3]; 60 | } 61 | if(args.length > 4){ 62 | repeat = Integer.parseInt(args[4]); 63 | } 64 | 65 | final ZMQ.Context context = ZMQ.context(1); 66 | ZMQ.Socket socket = context.socket(ZMQ.DEALER); 67 | socket.connect(address); 68 | 69 | // Handshake 70 | try { 71 | Thread.sleep(1000); 72 | } catch (Exception e) { 73 | e.printStackTrace(); 74 | } 75 | 76 | long startTime = System.currentTimeMillis(); 77 | try { 78 | 79 | // Send one message 80 | if(repeat == 1){ 81 | socket.send(format(method, url, json, null), 0); 82 | 83 | byte[] response = socket.recv(0); 84 | System.out.println("Response: \r\n" + new String(response, "UTF-8")); 85 | 86 | // Send a lot of messages 87 | } else { 88 | for (int i = 0; i < repeat; i++) { 89 | byte[] message = format(method, url, json, i); 90 | socket.send(message, ZMQ.NOBLOCK); 91 | 92 | byte[] response = socket.recv(ZMQ.NOBLOCK); 93 | if ((response != null) && (response.length > 0)) { 94 | System.out.println("Response: \r\n" + new String(response, "UTF-8")); 95 | } 96 | } 97 | } 98 | } catch (UnsupportedEncodingException e) { 99 | e.printStackTrace(); 100 | } finally { 101 | try { 102 | socket.close(); 103 | } catch (Exception e2) { 104 | // ignore 105 | } 106 | try { 107 | context.term(); 108 | } catch (Exception e2) { 109 | // ignore 110 | } 111 | 112 | System.out.printf("%d message(s) sent in %dms", repeat, System.currentTimeMillis()-startTime); 113 | } 114 | } 115 | } 116 | -------------------------------------------------------------------------------- /src/main/java/org/elasticsearch/zeromq/ZMQRestImpl.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Licensed to Elastic Search and Shay Banon under one 3 | * or more contributor license agreements. See the NOTICE file 4 | * distributed with this work for additional information 5 | * regarding copyright ownership. Elastic Search licenses this 6 | * file to you under the Apache License, Version 2.0 (the 7 | * "License"); you may not use this file except in compliance 8 | * with the License. You may obtain a copy of the License at 9 | * 10 | * http://www.apache.org/licenses/LICENSE-2.0 11 | * 12 | * Unless required by applicable law or agreed to in writing, 13 | * software distributed under the License is distributed on an 14 | * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY 15 | * KIND, either express or implied. See the License for the 16 | * specific language governing permissions and limitations 17 | * under the License. 18 | */ 19 | 20 | package org.elasticsearch.zeromq; 21 | 22 | import java.io.IOException; 23 | import java.nio.ByteBuffer; 24 | import java.util.concurrent.CountDownLatch; 25 | import java.util.concurrent.atomic.AtomicReference; 26 | 27 | import org.elasticsearch.common.component.AbstractComponent; 28 | import org.elasticsearch.common.inject.Inject; 29 | import org.elasticsearch.common.settings.Settings; 30 | import org.elasticsearch.rest.RestChannel; 31 | import org.elasticsearch.rest.RestController; 32 | import org.elasticsearch.rest.RestResponse; 33 | import org.zeromq.ZMQException; 34 | 35 | /** 36 | * @author tlrx 37 | */ 38 | public class ZMQRestImpl extends AbstractComponent { 39 | 40 | private final RestController restController; 41 | 42 | @Inject 43 | public ZMQRestImpl(Settings settings, RestController restController) { 44 | super(settings); 45 | this.restController = restController; 46 | } 47 | 48 | public ZMQRestResponse process(ZMQRestRequest request){ 49 | 50 | final CountDownLatch latch = new CountDownLatch(1); 51 | final AtomicReference ref = new AtomicReference(); 52 | 53 | this.restController.dispatchRequest(request, new RestChannel() { 54 | 55 | @Override 56 | public void sendResponse(RestResponse response) { 57 | try { 58 | if(logger.isTraceEnabled()){ 59 | logger.info("Response to ØMQ client: {}", new String(response.content())); 60 | } 61 | ref.set(convert(response)); 62 | } catch (IOException e) { 63 | // ignore 64 | } 65 | latch.countDown(); 66 | } 67 | }); 68 | 69 | try { 70 | latch.await(); 71 | return ref.get(); 72 | } catch (Exception e) { 73 | throw new ZMQException("failed to generate response", 0); 74 | } 75 | } 76 | 77 | private ZMQRestResponse convert(RestResponse response) throws IOException { 78 | ZMQRestResponse zmqResponse = new ZMQRestResponse(response.status()); 79 | 80 | if(response.contentType() != null){ 81 | zmqResponse.setContentType(response.contentType()); 82 | } 83 | if (response.contentLength() > 0) { 84 | if (response.contentThreadSafe()) { 85 | zmqResponse.setBody(ByteBuffer.wrap(response.content(), 0, response.contentLength())); 86 | } else { 87 | // argh!, we need to copy it over since we are not on the same thread... 88 | byte[] body = new byte[response.contentLength()]; 89 | System.arraycopy(response.content(), 0, body, 0, response.contentLength()); 90 | zmqResponse.setBody(ByteBuffer.wrap(body)); 91 | } 92 | } 93 | return zmqResponse; 94 | } 95 | } 96 | -------------------------------------------------------------------------------- /src/main/java/org/elasticsearch/zeromq/network/ZMQAddressHelper.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Licensed to Elastic Search and Shay Banon under one 3 | * or more contributor license agreements. See the NOTICE file 4 | * distributed with this work for additional information 5 | * regarding copyright ownership. Elastic Search licenses this 6 | * file to you under the Apache License, Version 2.0 (the 7 | * "License"); you may not use this file except in compliance 8 | * with the License. You may obtain a copy of the License at 9 | * 10 | * http://www.apache.org/licenses/LICENSE-2.0 11 | * 12 | * Unless required by applicable law or agreed to in writing, 13 | * software distributed under the License is distributed on an 14 | * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY 15 | * KIND, either express or implied. See the License for the 16 | * specific language governing permissions and limitations 17 | * under the License. 18 | */ 19 | 20 | package org.elasticsearch.zeromq.network; 21 | 22 | /** 23 | * @author tlrx 24 | */ 25 | public class ZMQAddressHelper { 26 | 27 | private class Address { 28 | private String type = null; 29 | private String hostname = null; 30 | private int port = 0; 31 | 32 | public Address() { 33 | } 34 | 35 | public String getType() { 36 | return type; 37 | } 38 | 39 | public void setType(String type) { 40 | this.type = type; 41 | } 42 | 43 | public String getHostname() { 44 | return hostname; 45 | } 46 | 47 | public void setHostname(String hostname) { 48 | this.hostname = hostname; 49 | } 50 | 51 | public int getPort() { 52 | return port; 53 | } 54 | 55 | public void setPort(int port) { 56 | this.port = port; 57 | } 58 | } 59 | 60 | private Address resolve(String address) { 61 | 62 | if (address == null) { 63 | return null; 64 | } 65 | Address ad = new Address(); 66 | String[] splits = address.split(":"); 67 | 68 | if (splits == null || splits.length <= 1) { 69 | return null; 70 | 71 | } else { 72 | ad.setType(splits[0]); 73 | 74 | if ("tpc".equals(splits[0])) { 75 | 76 | String host = splits[1].replaceFirst("//", ""); 77 | if ("*".equals(host)) { 78 | ad.setHostname("localhost"); 79 | } else { 80 | ad.setHostname(host); 81 | } 82 | } 83 | 84 | if (splits.length >= 3) { 85 | int port = 0; 86 | try { 87 | port = Integer.parseInt(splits[2]); 88 | } catch (NumberFormatException e) { 89 | // Apply some default value. Do not let the "catch" block empty ! 90 | port = 0; 91 | } 92 | ad.setPort(port); 93 | } 94 | } 95 | return ad; 96 | } 97 | 98 | public static String getHostName(String address) { 99 | Address ad = new ZMQAddressHelper().resolve(address); 100 | 101 | if (ad != null) { 102 | return ad.getHostname(); 103 | } else { 104 | return null; 105 | } 106 | 107 | } 108 | 109 | public static int getPort(String address) { 110 | Address ad = new ZMQAddressHelper().resolve(address); 111 | 112 | if (ad != null) { 113 | return ad.getPort(); 114 | } else { 115 | return 0; 116 | } 117 | 118 | } 119 | 120 | } 121 | -------------------------------------------------------------------------------- /src/main/java/org/elasticsearch/zeromq/ZMQRestRequest.java: -------------------------------------------------------------------------------- 1 | package org.elasticsearch.zeromq; 2 | 3 | import org.elasticsearch.common.Bytes; 4 | import org.elasticsearch.common.Unicode; 5 | import org.elasticsearch.rest.support.AbstractRestRequest; 6 | import org.elasticsearch.rest.support.RestUtils; 7 | import org.elasticsearch.zeromq.exception.NoURIFoundZMQException; 8 | import org.elasticsearch.zeromq.exception.UnsupportedMethodZMQException; 9 | import org.elasticsearch.zeromq.exception.ZMQTransportException; 10 | 11 | import java.nio.ByteBuffer; 12 | import java.util.HashMap; 13 | import java.util.List; 14 | import java.util.Map; 15 | 16 | /** 17 | * @author tlrx 18 | * 19 | */ 20 | public class ZMQRestRequest extends AbstractRestRequest { 21 | 22 | private final List parts; 23 | 24 | private Method method; 25 | 26 | private String uri; 27 | 28 | private String rawPath; 29 | 30 | private final Map params; 31 | 32 | public ByteBuffer body; 33 | 34 | public ZMQRestRequest(String payload, List parts) { 35 | super(); 36 | this.parts = parts; 37 | this.params = new HashMap(); 38 | 39 | parse(payload); 40 | } 41 | 42 | private void parse(String payload) { 43 | 44 | if (payload != null) { 45 | 46 | String[] s = payload.split("\\|"); 47 | 48 | if(s.length <2){ 49 | throw new ZMQTransportException("Invalid message format"); 50 | } 51 | 52 | // Method 53 | String m = s[0]; 54 | 55 | if ("GET".equalsIgnoreCase(m)) { 56 | this.method = Method.GET; 57 | } else if ("POST".equalsIgnoreCase(m)) { 58 | this.method = Method.POST; 59 | } else if ("PUT".equalsIgnoreCase(m)) { 60 | this.method = Method.PUT; 61 | } else if ("DELETE".equalsIgnoreCase(m)) { 62 | this.method = Method.DELETE; 63 | } else if ("OPTIONS".equalsIgnoreCase(m)) { 64 | this.method = Method.OPTIONS; 65 | } else if ("HEAD".equalsIgnoreCase(m)) { 66 | this.method = Method.HEAD; 67 | } else { 68 | throw new UnsupportedMethodZMQException(m); 69 | } 70 | 71 | // URI 72 | this.uri = s[1]; 73 | 74 | if((this.uri == null) || ("".equals(this.uri)) || "null".equalsIgnoreCase(this.uri)){ 75 | throw new NoURIFoundZMQException(); 76 | } 77 | 78 | int pathEndPos = uri.indexOf('?'); 79 | if (pathEndPos < 0) { 80 | this.rawPath = uri; 81 | } else { 82 | this.rawPath = uri.substring(0, pathEndPos); 83 | RestUtils.decodeQueryString(uri, pathEndPos + 1, params); 84 | } 85 | 86 | // Content 87 | int indexContent = payload.indexOf(ZMQSocket.SEPARATOR, m.length() + uri.length()); 88 | body = ByteBuffer.wrap(payload.substring(indexContent+1).getBytes()); 89 | } 90 | } 91 | 92 | @Override 93 | public Method method() { 94 | return this.method; 95 | } 96 | 97 | @Override 98 | public String uri() { 99 | return this.uri; 100 | } 101 | 102 | @Override 103 | public String rawPath() { 104 | return this.rawPath; 105 | } 106 | 107 | @Override 108 | public boolean hasContent() { 109 | return ((body != null) && (body.remaining() > 0)); 110 | } 111 | 112 | @Override 113 | public boolean contentUnsafe() { 114 | return false; 115 | } 116 | 117 | @Override 118 | public byte[] contentByteArray() { 119 | if (body == null) { 120 | return Bytes.EMPTY_ARRAY; 121 | } 122 | return body.array(); 123 | } 124 | 125 | @Override 126 | public int contentByteArrayOffset() { 127 | if (body == null) { 128 | return 0; 129 | } 130 | return body.arrayOffset() + body.position(); 131 | } 132 | 133 | @Override 134 | public int contentLength() { 135 | if (body == null) { 136 | return 0; 137 | } 138 | return body.remaining(); 139 | } 140 | 141 | @Override 142 | public String contentAsString() { 143 | if (body == null) { 144 | return ""; 145 | } 146 | return Unicode.fromBytes(contentByteArray(), contentByteArrayOffset(), 147 | contentLength()); 148 | } 149 | 150 | @Override 151 | public String header(String name) { 152 | return null; 153 | } 154 | 155 | @Override 156 | public boolean hasParam(String key) { 157 | return params.containsKey(key); 158 | } 159 | 160 | @Override 161 | public String param(String key) { 162 | 163 | String p = params.get(key); 164 | return p; 165 | } 166 | 167 | @Override 168 | public Map params() { 169 | return params; 170 | } 171 | 172 | @Override 173 | public String param(String key, String defaultValue) { 174 | String value = params.get(key); 175 | if (value == null) { 176 | return defaultValue; 177 | } 178 | return value; 179 | } 180 | 181 | } 182 | -------------------------------------------------------------------------------- /src/main/java/org/elasticsearch/zeromq/ZMQSocket.java: -------------------------------------------------------------------------------- 1 | package org.elasticsearch.zeromq; 2 | 3 | import java.util.ArrayList; 4 | import java.util.List; 5 | import java.util.concurrent.CountDownLatch; 6 | import java.util.concurrent.atomic.AtomicBoolean; 7 | 8 | import org.elasticsearch.common.logging.ESLogger; 9 | import org.elasticsearch.zeromq.impl.ZMQQueueServerImpl; 10 | import org.zeromq.ZMQ; 11 | import org.zeromq.ZMQ.Context; 12 | import org.zeromq.ZMQException; 13 | 14 | /** 15 | * @author tlrx 16 | * 17 | */ 18 | public class ZMQSocket implements Runnable { 19 | 20 | public final static String SEPARATOR = "|"; 21 | 22 | private final ESLogger logger; 23 | 24 | private ZMQ.Socket socket; 25 | 26 | private volatile ZMQ.Context context; 27 | 28 | final String workersBinding; 29 | 30 | final int id; 31 | 32 | private final ZMQRestImpl client; 33 | 34 | private final AtomicBoolean isRunning; 35 | 36 | private final CountDownLatch waitForSocketsClose; 37 | 38 | public ZMQSocket(ESLogger logger, Context context, String workersBinding, int id, ZMQRestImpl client, AtomicBoolean isRunning, CountDownLatch waitForSocketsClose) { 39 | super(); 40 | this.context = context; 41 | this.workersBinding = workersBinding; 42 | this.id = id; 43 | this.logger = logger; 44 | this.client = client; 45 | this.isRunning = isRunning; 46 | this.waitForSocketsClose = waitForSocketsClose; 47 | } 48 | 49 | @Override 50 | public void run() { 51 | 52 | socket = context.socket(ZMQ.ROUTER); 53 | socket.connect(workersBinding); 54 | 55 | if (logger.isInfoEnabled()) { 56 | logger.info("ØMQ socket {} is listening...", id); 57 | } 58 | 59 | while (isRunning.get()) { 60 | 61 | // Reads all parts of the message 62 | List parts = new ArrayList(); 63 | 64 | try { 65 | 66 | do { 67 | byte[] request = socket.recv(0); 68 | parts.add(request); 69 | }while (socket.hasReceiveMore()); 70 | 71 | } catch (ZMQException zmqe) { 72 | // Close the socket 73 | if(logger.isWarnEnabled()){ 74 | logger.warn("Exception when receiving message", zmqe); 75 | } 76 | } 77 | 78 | if(parts.isEmpty()){ 79 | continue; 80 | } 81 | 82 | // Payload 83 | String payload = new String(parts.get(parts.size() - 1)); 84 | 85 | if(logger.isDebugEnabled()){ 86 | logger.debug("ØMQ socket {} receives message: {}", id, payload); 87 | } 88 | 89 | ZMQRestResponse response = null; 90 | ZMQRestRequest request = null; 91 | 92 | // Stores the latest exception 93 | Exception lastException = null; 94 | 95 | if(ZMQQueueServerImpl.ZMQ_STOP_SOCKET.equals(payload)){ 96 | if(logger.isInfoEnabled()){ 97 | logger.info("ØMQ socket {} receives stop message", id); 98 | } 99 | 100 | } else { 101 | try{ 102 | // Construct an ES request 103 | request = new ZMQRestRequest(payload, parts); 104 | 105 | // Process the request 106 | response = client.process(request); 107 | 108 | }catch (Exception e){ 109 | if(logger.isErrorEnabled()){ 110 | logger.error("Exception when processing ØMQ message", e); 111 | } 112 | response = null; 113 | lastException = e; 114 | } 115 | } 116 | 117 | // Sends all the message parts back 118 | for(int i=0; i<(parts.size() - 1); i++){ 119 | socket.send(parts.get(i), ZMQ.SNDMORE); 120 | } 121 | 122 | // Sends the reply 123 | if (response != null) { 124 | socket.send(response.payload(), 0); 125 | 126 | } else if(lastException != null) { 127 | // An error occured 128 | socket.send(("Unable to process ØMQ message [" + lastException.getMessage() + "]").getBytes(), 0); 129 | 130 | } else { 131 | // Should not happen except when stop message is received 132 | socket.send(("Unable to process ØMQ message or stop socket message received").getBytes(), 0); 133 | } 134 | } 135 | 136 | try { 137 | if (logger.isDebugEnabled()) { 138 | logger.debug("Closing ØMQ socket {}", id); 139 | } 140 | 141 | // Close the socket 142 | socket.close(); 143 | logger.info("ØMQ socket {} is closed", id); 144 | 145 | // Decrement the countdownlatch 146 | this.waitForSocketsClose.countDown(); 147 | 148 | } catch (Exception e) { 149 | logger.error("Exception when closing ØMQ socket", e); 150 | } 151 | } 152 | } 153 | -------------------------------------------------------------------------------- /README.textile: -------------------------------------------------------------------------------- 1 | h1. ØMQ transport layer plugin for Elasticsearch 2 | 3 | This plugin exposes the REST interfaces of "Elasticsearch":http://www.elasticsearch.org over "ZeroMQ":http://www.zeromq.org/ messaging library. 4 | 5 | h2. Versions 6 | 7 | |_. ØMQ Transport Plugin|_. ElasticSearch |_. ØMQ | 8 | | master (0.0.3) | master (0.19.2) | 2.2 | 9 | | 0.0.2 | 0.18.2 | 2.1 | 10 | 11 | 12 | h2. Installation 13 | 14 | h3. Requirements 15 | 16 | Before installing and using this plugin with Elasticsearch (0.19.2), you need to install ZeroMQ Java Binding library (2.2) on your system. This binding uses native library to work. 17 | 18 | The "ZeroMQ":http://www.zeromq.org/ website is a great place to start installing the libraries, specially the "Java binding page":http://www.zeromq.org/bindings:java 19 | 20 | If you want to develop or modify this plugin, I encourage you to read the "ØMQ - The Guide":http://zguide.zeromq.org/page:all which is very well documented. 21 | 22 | h3. Installation 23 | 24 | Type the command in your favorite shell : 25 | 26 |
 27 | $ bin\plugin -install tlrx/transport-zeromq/0.0.3
 28 | 
29 | 30 | Elasticsearch automatically install the plugin: 31 | 32 |
 33 | -> Installing tlrx/transport-zeromq/0.0.3...
 34 | Trying https://github.com/downloads/tlrx/transport-zeromq/transport-zeromq-0.0.3.zip...
 35 | Downloading ..........DONE
 36 | Installed transport-zeromq
 37 | 
38 | 39 | Then, you must replace the file *plugins/transport-zeromq/jzmq-1.0.0.jar* with the Java binding compiled for your system. 40 | 41 | Finally, edit Elasticsearch configuration file @config/elasticsearch.yml@ and add the properties: 42 | 43 |
 44 | # ZeroMQ Transport config
 45 | zeromq.router.bind: tcp://*:9700
 46 | zeromq.workers.threads: 2
 47 | zeromq.workers.bind: inproc://es_zeromq_workers
 48 | 
49 | 50 | Restart Elasticsearch. 51 | 52 | If you have the following error 53 |
Initialization Failed ...
 54 | 1) UnsatisfiedLinkError[no jzmq in java.library.path]2) NoClassDefFoundError[Could not initialize class org.zeromq.ZMQ]
55 | 56 | You can try to update your LD_LIBRARY_PATH: 57 |
export LD_LIBRARY_PATH=$LD_LIBRARY_PATH:/usr/local/lib
58 | 59 | You can also read the documentation of the "ØMQ Java binding":http://www.zeromq.org/bindings:java. 60 | 61 | 62 | h2. Behind the scene 63 | 64 | The plugin exposes the REST interfaces of Elasticsearch over ØMQ sockets. The implementation uses a "router-dealer pattern":http://www.zeromq.org/sandbox:dealer, where multiple ROUTER sockets (2 by default, see @zeromq.workers.threads@) are listening to incoming messages (each in a dedicated thread) send by DEALER sockets on the @zeromq.router.bind@ address. This way, it is possible to send REST-like messages with ØMQ clients and get the replies back. 65 | 66 | For example, a ØMQ client can send the following message: 67 |
POST|/twitter/tweet/2|{"user" : "kimchy", "post_date" : "2009-11-15T14:12:12", "message" : "You know, for Search"}
68 | 69 | It will receive the following response back: 70 |
201|CREATED|{"ok" : true, "_index" : "twitter", "_type" : "tweet", "_id" : "2", "_version" : 1}
71 | 72 | The transport layer converts ØMQ messages in a given format into REST request objects that can be handled by ES. 73 | 74 | The expected format for incoming messages is: 75 |
 || 
76 | 77 | The format for outcoming message is: 78 |
 || 
79 | 80 | But any other message format can be easely implemented if needed. 81 | 82 | 83 | 84 | h3. Simple ØMQ client to test the plugin 85 | 86 | The @SimpleClient@ Java class in test package shows how to create a simple ØMQ client and send messages. In your test, take care to add the native library to classpath (@-Djava.library.path=/usr/local/lib@). 87 | 88 | h4. Add new document 89 | 90 | Message: 91 |
java org.elasticsearch.zeromq.test.SimpleClient tcp://localhost:9700 PUT /twitter/tweet/2 "{\"user\": \"kimchy\",\"post_date\": \"2009-11-15T14:12:12\",\"message\": \"You know, for Search\"}"
92 | 93 | Reply: 94 |
201|CREATED|{"ok" : true, "_index" : "twitter", "_type" : "tweet", "_id" : "2", "_version" : 1}
95 | 96 | 97 | h4. Search for document 98 | 99 | Message: 100 |
java org.elasticsearch.zeromq.test.SimpleClient tcp://localhost:9700 GET /twitter/tweet/_search?q=user:kimchy
101 | 102 | Reply: 103 |
200|OK|{"took" : 29, "timed_out" : false, "_shards" : {"total" : 5, "successful" : 5, "failed" : 0}, "hits" : {"total" : 1, "max_score" : 0.30685282, "hits" : [{"_index" : "twitter", "_type" : "tweet", "_id" : "2", "_score" : 0.30685282, "_source" : {
104 |     "user" : "kimchy",
105 |     "post_date" : "2009-11-15T14:12:12",
106 |     "message" : "You know, for Search"
107 | }}]}}
108 | 
109 | 110 | 111 | h4. Delete a document 112 | 113 | Message: 114 |
java org.elasticsearch.zeromq.test.SimpleClient tcp://localhost:9700 DELETE /twitter/tweet/2
115 | 116 | Reply: 117 |
200|OK|{"ok" : true, "found" : true, "_index" : "twitter", "_type" : "tweet", "_id" : "2", "_version" : 2}
118 | 119 | 120 | h3. Other examples 121 | 122 | The @ZMQTransportPluginTest@ Java class in test package has other examples. 123 | 124 | ----- 125 | 126 | This software is licensed under the Apache License, version 2 ("ALv2"). 127 | 128 | 129 | Thanks to "David Pilato":https://github.com/dadoonet for the Maven pom and README files. 130 | 131 | -------------------------------------------------------------------------------- /pom.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 4.0.0 5 | org.elasticsearch.plugin 6 | transport-zeromq 7 | jar 8 | 0.0.4-SNAPSHOT 9 | ØMQ transport layer plugin for Elasticsearch 10 | https://github.com/tlrx/transport-zeromq 11 | 12 | 13 | 0.19.2 14 | 15 | 16 | 17 | scm:git:git@github.com:tlrx/transport-zeromq.git 18 | scm:git:git@github.com:tlrx/transport-zeromq.git 19 | scm:git:git@github.com:tlrx/transport-zeromq.git 20 | 21 | 22 | 23 | GitHub 24 | https://github.com/tlrx/transport-zeromq/issues/ 25 | 26 | 27 | 28 | 29 | log4j 30 | log4j 31 | 1.2.16 32 | 33 | 34 | junit 35 | junit 36 | 4.10 37 | 38 | 39 | org.elasticsearch 40 | elasticsearch 41 | ${elasticsearch.version} 42 | 43 | 44 | org.elasticsearch.zeromq 45 | jzmq 46 | 1.0.0 47 | system 48 | ${project.basedir}/jzmq-1.0.0.jar 49 | 50 | 51 | 52 | 53 | 54 | 55 | com.github.github 56 | downloads-maven-plugin 57 | 0.3 58 | 59 | ${project.name} build of the ${project.version} release 60 | true 61 | true 62 | false 63 | 64 | **/*.zip 65 | 66 | 67 | **/*SNAPSHOT* 68 | 69 | 70 | 71 | 72 | 73 | upload 74 | 75 | deploy 76 | 77 | 78 | 79 | 80 | org.apache.maven.plugins 81 | maven-release-plugin 82 | 2.2.1 83 | 84 | 85 | 86 | org.apache.maven.plugins 87 | maven-jar-plugin 88 | 2.3.2 89 | 90 | 91 | 92 | org.apache.maven.plugins 93 | maven-dependency-plugin 94 | 2.3 95 | 96 | 97 | copy-dependencies 98 | package 99 | 100 | copy-dependencies 101 | 102 | 103 | ${project.build.directory} 104 | system 105 | 106 | 107 | 108 | 109 | 110 | 111 | org.apache.maven.plugins 112 | maven-assembly-plugin 113 | 2.2.1 114 | 115 | false 116 | ${project.build.directory}/release/ 117 | 118 | ${basedir}/src/main/assemblies/esplugin.xml 119 | 120 | 121 | 122 | 123 | generate-release-plugin 124 | package 125 | 126 | single 127 | 128 | 129 | 130 | 131 | 132 | org.apache.maven.plugins 133 | maven-compiler-plugin 134 | 2.3.2 135 | 136 | 1.6 137 | 1.6 138 | 139 | 140 | 141 | 142 | 143 | src/main/resources 144 | true 145 | 146 | **/*.properties 147 | 148 | 149 | 150 | 151 | 152 | 153 | 154 | sonatype 155 | Sonatype Groups 156 | https://oss.sonatype.org/content/groups/public/ 157 | 158 | 159 | 160 | 161 | 162 | local-tmp-repo 163 | Local Temporary Reposioty 164 | file:///tmp/local_tmp_repo/ 165 | 166 | 167 | 168 | -------------------------------------------------------------------------------- /src/test/java/org/elasticsearch/zeromq/test/ZMQTransportPluginTest.java: -------------------------------------------------------------------------------- 1 | package org.elasticsearch.zeromq.test; 2 | 3 | import static org.elasticsearch.common.xcontent.XContentFactory.jsonBuilder; 4 | 5 | import java.io.IOException; 6 | import java.io.UnsupportedEncodingException; 7 | import java.nio.charset.Charset; 8 | import java.util.Date; 9 | 10 | import org.elasticsearch.common.settings.ImmutableSettings; 11 | import org.elasticsearch.common.xcontent.XContentBuilder; 12 | import org.elasticsearch.node.Node; 13 | import org.elasticsearch.node.NodeBuilder; 14 | import org.elasticsearch.zeromq.ZMQSocket; 15 | import org.junit.AfterClass; 16 | import org.junit.Assert; 17 | import org.junit.BeforeClass; 18 | import org.junit.Test; 19 | import org.zeromq.ZMQ; 20 | 21 | public class ZMQTransportPluginTest { 22 | 23 | private static Node node = null; 24 | 25 | private static ZMQ.Context context = null; 26 | 27 | /* 28 | * ØMQ Socket binding adress, must be coherent with elasticsearch.yml config file 29 | */ 30 | private static final String address = "tcp://localhost:9800"; 31 | 32 | @BeforeClass 33 | public static void setUpBeforeClass() throws Exception { 34 | // Instantiate an ES server 35 | node = NodeBuilder.nodeBuilder() 36 | .settings( 37 | ImmutableSettings.settingsBuilder() 38 | .put("es.config", "elasticsearch.yml") 39 | ).node(); 40 | 41 | // Instantiate a ZMQ context 42 | context = ZMQ.context(1); 43 | } 44 | 45 | @AfterClass 46 | public static void tearDownAfterClass() throws Exception { 47 | if(node != null){ 48 | node.close(); 49 | } 50 | 51 | try { 52 | context.term(); 53 | } catch (Exception e2) { 54 | // ignore 55 | } 56 | } 57 | 58 | /** 59 | * Simple method to send & receive zeromq message 60 | * 61 | * @param method 62 | * @param uri 63 | * @param json 64 | * @return 65 | */ 66 | private String sendAndReceive(String method, String uri, String json){ 67 | 68 | ZMQ.Socket socket = context.socket(ZMQ.DEALER); 69 | socket.connect(address); 70 | 71 | // Handshake 72 | try { 73 | Thread.sleep(100); 74 | } catch (Exception e) { 75 | Assert.fail("Handshake failed"); 76 | } 77 | 78 | StringBuilder sb = new StringBuilder(method); 79 | sb.append(ZMQSocket.SEPARATOR).append(uri).append(ZMQSocket.SEPARATOR); 80 | 81 | if(json != null){ 82 | sb.append(json); 83 | } 84 | 85 | String result = null; 86 | try { 87 | socket.send(sb.toString().getBytes("UTF-8"), 0); 88 | 89 | byte[] response = socket.recv(0); 90 | result = new String(response, Charset.forName("UTF-8")); 91 | 92 | } catch (UnsupportedEncodingException e) { 93 | Assert.fail("Exception when sending/receiving message"); 94 | } finally { 95 | try { 96 | socket.close(); 97 | } catch (Exception e2) { 98 | // ignore 99 | } 100 | } 101 | return result; 102 | } 103 | 104 | 105 | @Test 106 | public void testDeleteMissingIndex(){ 107 | String response = sendAndReceive("DELETE", "/test-index-missing/", null); 108 | Assert.assertEquals("404|NOT_FOUND|{\"error\":\"IndexMissingException[[test-index-missing] missing]\",\"status\":404}", response); 109 | } 110 | 111 | @Test 112 | public void testCreateIndex(){ 113 | String response = sendAndReceive("DELETE", "/books/", null); 114 | Assert.assertNotNull(response); 115 | 116 | response = sendAndReceive("PUT", "/books/", null); 117 | Assert.assertEquals("200|OK|{\"ok\":true,\"acknowledged\":true}", response); 118 | } 119 | 120 | @Test 121 | public void testMapping() throws IOException{ 122 | XContentBuilder mapping = jsonBuilder() 123 | .startObject() 124 | .startObject("book") 125 | .startObject("properties") 126 | .startObject("title") 127 | .field("type", "string") 128 | .field("analyzer", "french") 129 | .endObject() 130 | .startObject("author") 131 | .field("type", "string") 132 | .endObject() 133 | .startObject("year") 134 | .field("type", "integer") 135 | .endObject() 136 | .startObject("publishedDate") 137 | .field("type", "date") 138 | .endObject() 139 | .endObject() 140 | .endObject() 141 | .endObject(); 142 | 143 | String response = sendAndReceive("PUT", "/books/book/_mapping", mapping.string()); 144 | Assert.assertEquals("200|OK|{\"ok\":true,\"acknowledged\":true}", response); 145 | } 146 | 147 | @Test 148 | public void testIndex() throws IOException{ 149 | XContentBuilder book1 = jsonBuilder() 150 | .startObject() 151 | .field("title", "Les Misérables") 152 | .field("author", "Victor Hugo") 153 | .field("year", "1862") 154 | .field("publishedDate", new Date()) 155 | .endObject(); 156 | 157 | String response = sendAndReceive("PUT", "/books/book/1", book1.string()); 158 | Assert.assertEquals("201|CREATED|{\"ok\":true,\"_index\":\"books\",\"_type\":\"book\",\"_id\":\"1\",\"_version\":1}", response); 159 | 160 | XContentBuilder book2 = jsonBuilder() 161 | .startObject() 162 | .field("title", "Notre-Dame de Paris") 163 | .field("author", "Victor Hugo") 164 | .field("year", "1831") 165 | .field("publishedDate", new Date()) 166 | .endObject(); 167 | 168 | response = sendAndReceive("PUT", "/books/book/2", book2.string()); 169 | Assert.assertEquals("201|CREATED|{\"ok\":true,\"_index\":\"books\",\"_type\":\"book\",\"_id\":\"2\",\"_version\":1}", response); 170 | 171 | XContentBuilder book3 = jsonBuilder() 172 | .startObject() 173 | .field("title", "Le Dernier Jour d'un condamné") 174 | .field("author", "Victor Hugo") 175 | .field("year", "1829") 176 | .field("publishedDate", new Date()) 177 | .endObject(); 178 | 179 | response = sendAndReceive("POST", "/books/book", book3.string()); 180 | Assert.assertNotNull("Response should not be null", response); 181 | Assert.assertTrue(response.startsWith("201|CREATED|{\"ok\":true,\"_index\":\"books\",\"_type\":\"book\",\"_id\"")); 182 | } 183 | 184 | @Test 185 | public void testRefresh() throws IOException{ 186 | String response = sendAndReceive("GET", "/_all/_refresh", null); 187 | Assert.assertTrue(response.startsWith("200|OK")); 188 | } 189 | 190 | @Test 191 | public void testSearch() throws IOException{ 192 | String response = sendAndReceive("GET", "/_all/_search", "{\"query\":{\"match_all\":{}}}"); 193 | Assert.assertTrue(response.contains("\"hits\":{\"total\":3")); 194 | 195 | response = sendAndReceive("GET", "_search", "{\"query\":{\"bool\":{\"must\":[{\"range\":{\"year\":{\"gte\":1820,\"lte\":1832}}}],\"must_not\":[],\"should\":[]}},\"from\":0,\"size\":50,\"sort\":[],\"facets\":{},\"version\":true}:"); 196 | Assert.assertTrue(response.contains("\"hits\":{\"total\":2")); 197 | } 198 | 199 | @Test 200 | public void testGet() throws IOException{ 201 | String response = sendAndReceive("GET", "/books/book/2", null); 202 | Assert.assertTrue(response.contains("Notre-Dame de Paris")); 203 | } 204 | } 205 | -------------------------------------------------------------------------------- /src/main/java/org/elasticsearch/zeromq/impl/ZMQQueueServerImpl.java: -------------------------------------------------------------------------------- 1 | /** 2 | * 3 | */ 4 | package org.elasticsearch.zeromq.impl; 5 | 6 | import static org.elasticsearch.common.util.concurrent.EsExecutors.daemonThreadFactory; 7 | 8 | import java.io.IOException; 9 | import java.net.InetSocketAddress; 10 | import java.util.concurrent.CopyOnWriteArrayList; 11 | import java.util.concurrent.CountDownLatch; 12 | import java.util.concurrent.TimeUnit; 13 | import java.util.concurrent.atomic.AtomicBoolean; 14 | 15 | import org.elasticsearch.ElasticSearchException; 16 | import org.elasticsearch.common.component.AbstractLifecycleComponent; 17 | import org.elasticsearch.common.inject.Inject; 18 | import org.elasticsearch.common.network.NetworkService; 19 | import org.elasticsearch.common.settings.Settings; 20 | import org.elasticsearch.common.transport.BoundTransportAddress; 21 | import org.elasticsearch.common.transport.InetSocketTransportAddress; 22 | import org.elasticsearch.http.BindHttpException; 23 | import org.elasticsearch.node.service.NodeService; 24 | import org.elasticsearch.transport.BindTransportException; 25 | import org.elasticsearch.zeromq.ZMQRestImpl; 26 | import org.elasticsearch.zeromq.ZMQServerTransport; 27 | import org.elasticsearch.zeromq.ZMQSocket; 28 | import org.elasticsearch.zeromq.network.ZMQAddressHelper; 29 | import org.zeromq.ZMQ; 30 | import org.zeromq.ZMQQueue; 31 | 32 | /** 33 | * Implementation of {@link ZMQServerTransport} based on a Router-Dealer-Queue 34 | * pattern. 35 | * 36 | * @author tlrx 37 | * 38 | */ 39 | public class ZMQQueueServerImpl extends 40 | AbstractLifecycleComponent implements 41 | ZMQServerTransport { 42 | 43 | final String routerBinding; 44 | 45 | final int nbWorkers; 46 | 47 | final String workersBinding; 48 | 49 | private final ZMQ.Context context; 50 | 51 | private ZMQ.Socket dealer; 52 | 53 | private ZMQ.Socket router; 54 | 55 | private Thread queueThread; 56 | 57 | private final ZMQRestImpl client; 58 | 59 | private CopyOnWriteArrayList sockets = new CopyOnWriteArrayList(); 60 | 61 | private final NetworkService networkService; 62 | 63 | private final NodeService nodeService; 64 | 65 | private volatile BoundTransportAddress boundAddress; 66 | 67 | private final AtomicBoolean isRunning; 68 | 69 | public static final String ZMQ_STOP_SOCKET = "stop"; 70 | 71 | private static volatile CountDownLatch waitForSocketsClose; 72 | 73 | @Inject 74 | protected ZMQQueueServerImpl(Settings settings, NodeService nodeService, ZMQRestImpl client, NetworkService networkService) { 75 | super(settings); 76 | this.client = client; 77 | this.networkService = networkService; 78 | this.nodeService = nodeService; 79 | 80 | logger.debug("Reading ØMQ transport layer settings..."); 81 | 82 | routerBinding = settings.get("zeromq.router.bind", "tcp://127.0.0.1:9700"); 83 | nbWorkers = settings.getAsInt("zeromq.workers.threads", 3); 84 | workersBinding = settings.get("zeromq.workers.bind", "inproc://es_zeromq_workers"); 85 | 86 | logger.debug( 87 | "ØMQ settings [zeromq.router.bind={}, zeromq.workers.threads={}, zeromq.workers.bind={}]", 88 | routerBinding, nbWorkers, workersBinding); 89 | 90 | logger.info("Creating ØMQ server context..."); 91 | context = ZMQ.context(1); 92 | 93 | isRunning = new AtomicBoolean(true); 94 | } 95 | 96 | @Override 97 | protected void doStart() throws ElasticSearchException { 98 | 99 | logger.debug("Starting ØMQ dealer socket..."); 100 | dealer = context.socket(ZMQ.DEALER); 101 | dealer.bind(workersBinding); 102 | 103 | InetSocketAddress bindAddress; 104 | try { 105 | bindAddress = new InetSocketAddress(networkService.resolveBindHostAddress(ZMQAddressHelper.getHostName(workersBinding)), ZMQAddressHelper.getPort(workersBinding)); 106 | } catch (IOException e) { 107 | throw new BindHttpException("Failed to resolve host [" + workersBinding + "]", e); 108 | } 109 | 110 | waitForSocketsClose = new CountDownLatch(nbWorkers); 111 | 112 | for (int i = 0; i < nbWorkers; i++) { 113 | 114 | logger.debug("Creating worker #{}", i); 115 | ZMQSocket worker = new ZMQSocket(logger, context, workersBinding, i, client, isRunning, waitForSocketsClose); 116 | 117 | daemonThreadFactory(settings, "zeromq_worker_" + i).newThread(worker).start(); 118 | 119 | sockets.add(worker); 120 | } 121 | 122 | logger.debug("Starting ØMQ router socket..."); 123 | router = context.socket(ZMQ.ROUTER); 124 | router.bind(routerBinding); 125 | 126 | InetSocketAddress publishAddress; 127 | try { 128 | publishAddress = new InetSocketAddress(networkService.resolvePublishHostAddress(ZMQAddressHelper.getHostName(routerBinding)), ZMQAddressHelper.getPort(routerBinding)); 129 | } catch (Exception e) { 130 | throw new BindTransportException("Failed to resolve publish address", e); 131 | } 132 | this.boundAddress = new BoundTransportAddress(new InetSocketTransportAddress(bindAddress), new InetSocketTransportAddress(publishAddress)); 133 | 134 | if (logger.isInfoEnabled()) { 135 | logger.info("{}", this.boundAddress); 136 | } 137 | nodeService.putAttribute("zeromq_address", this.boundAddress.publishAddress().toString()); 138 | 139 | logger.debug("Starting ØMQ queue..."); 140 | queueThread = new Thread(new ZMQQueue(context, router, dealer)); 141 | queueThread.start(); 142 | 143 | logger.info("ØMQ server started"); 144 | } 145 | 146 | @Override 147 | protected void doClose() throws ElasticSearchException { 148 | logger.info("Closing ØMQ server..."); 149 | 150 | // After next incoming message, sockets will close themselves 151 | isRunning.set(false); 152 | 153 | while(ZMQQueueServerImpl.waitForSocketsClose.getCount() > 0){ 154 | 155 | // Let's send a stop message to the sockets 156 | dealer.send(ZMQ_STOP_SOCKET.getBytes(), 0); 157 | 158 | // Wait a few 159 | try { 160 | waitForSocketsClose.await(50, TimeUnit.MILLISECONDS); 161 | } catch (InterruptedException e) { 162 | // nothing 163 | } 164 | 165 | // Receive a response... or not 166 | dealer.recv(ZMQ.NOBLOCK); 167 | } 168 | 169 | // Stops the queue 170 | queueThread.interrupt(); 171 | logger.info("ØMQ queue thread interrupted"); 172 | 173 | // Stop the router socket, no accept message anymore 174 | router.close(); 175 | logger.info("ØMQ router socket closed"); 176 | 177 | // Close dealer socket 178 | dealer.close(); 179 | logger.info("ØMQ dealer socket closed"); 180 | 181 | context.term(); 182 | logger.info("ØMQ server closed"); 183 | } 184 | 185 | @Override 186 | protected void doStop() throws ElasticSearchException { 187 | logger.debug("Stopping ØMQ server..."); 188 | } 189 | 190 | @Override public BoundTransportAddress boundAddress() { 191 | return boundAddress; 192 | } 193 | } 194 | -------------------------------------------------------------------------------- /LICENSE.txt: -------------------------------------------------------------------------------- 1 | 2 | Apache License 3 | Version 2.0, January 2004 4 | http://www.apache.org/licenses/ 5 | 6 | TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION 7 | 8 | 1. Definitions. 9 | 10 | "License" shall mean the terms and conditions for use, reproduction, 11 | and distribution as defined by Sections 1 through 9 of this document. 12 | 13 | "Licensor" shall mean the copyright owner or entity authorized by 14 | the copyright owner that is granting the License. 15 | 16 | "Legal Entity" shall mean the union of the acting entity and all 17 | other entities that control, are controlled by, or are under common 18 | control with that entity. For the purposes of this definition, 19 | "control" means (i) the power, direct or indirect, to cause the 20 | direction or management of such entity, whether by contract or 21 | otherwise, or (ii) ownership of fifty percent (50%) or more of the 22 | outstanding shares, or (iii) beneficial ownership of such entity. 23 | 24 | "You" (or "Your") shall mean an individual or Legal Entity 25 | exercising permissions granted by this License. 26 | 27 | "Source" form shall mean the preferred form for making modifications, 28 | including but not limited to software source code, documentation 29 | source, and configuration files. 30 | 31 | "Object" form shall mean any form resulting from mechanical 32 | transformation or translation of a Source form, including but 33 | not limited to compiled object code, generated documentation, 34 | and conversions to other media types. 35 | 36 | "Work" shall mean the work of authorship, whether in Source or 37 | Object form, made available under the License, as indicated by a 38 | copyright notice that is included in or attached to the work 39 | (an example is provided in the Appendix below). 40 | 41 | "Derivative Works" shall mean any work, whether in Source or Object 42 | form, that is based on (or derived from) the Work and for which the 43 | editorial revisions, annotations, elaborations, or other modifications 44 | represent, as a whole, an original work of authorship. For the purposes 45 | of this License, Derivative Works shall not include works that remain 46 | separable from, or merely link (or bind by name) to the interfaces of, 47 | the Work and Derivative Works thereof. 48 | 49 | "Contribution" shall mean any work of authorship, including 50 | the original version of the Work and any modifications or additions 51 | to that Work or Derivative Works thereof, that is intentionally 52 | submitted to Licensor for inclusion in the Work by the copyright owner 53 | or by an individual or Legal Entity authorized to submit on behalf of 54 | the copyright owner. For the purposes of this definition, "submitted" 55 | means any form of electronic, verbal, or written communication sent 56 | to the Licensor or its representatives, including but not limited to 57 | communication on electronic mailing lists, source code control systems, 58 | and issue tracking systems that are managed by, or on behalf of, the 59 | Licensor for the purpose of discussing and improving the Work, but 60 | excluding communication that is conspicuously marked or otherwise 61 | designated in writing by the copyright owner as "Not a Contribution." 62 | 63 | "Contributor" shall mean Licensor and any individual or Legal Entity 64 | on behalf of whom a Contribution has been received by Licensor and 65 | subsequently incorporated within the Work. 66 | 67 | 2. Grant of Copyright License. Subject to the terms and conditions of 68 | this License, each Contributor hereby grants to You a perpetual, 69 | worldwide, non-exclusive, no-charge, royalty-free, irrevocable 70 | copyright license to reproduce, prepare Derivative Works of, 71 | publicly display, publicly perform, sublicense, and distribute the 72 | Work and such Derivative Works in Source or Object form. 73 | 74 | 3. Grant of Patent License. Subject to the terms and conditions of 75 | this License, each Contributor hereby grants to You a perpetual, 76 | worldwide, non-exclusive, no-charge, royalty-free, irrevocable 77 | (except as stated in this section) patent license to make, have made, 78 | use, offer to sell, sell, import, and otherwise transfer the Work, 79 | where such license applies only to those patent claims licensable 80 | by such Contributor that are necessarily infringed by their 81 | Contribution(s) alone or by combination of their Contribution(s) 82 | with the Work to which such Contribution(s) was submitted. If You 83 | institute patent litigation against any entity (including a 84 | cross-claim or counterclaim in a lawsuit) alleging that the Work 85 | or a Contribution incorporated within the Work constitutes direct 86 | or contributory patent infringement, then any patent licenses 87 | granted to You under this License for that Work shall terminate 88 | as of the date such litigation is filed. 89 | 90 | 4. Redistribution. You may reproduce and distribute copies of the 91 | Work or Derivative Works thereof in any medium, with or without 92 | modifications, and in Source or Object form, provided that You 93 | meet the following conditions: 94 | 95 | (a) You must give any other recipients of the Work or 96 | Derivative Works a copy of this License; and 97 | 98 | (b) You must cause any modified files to carry prominent notices 99 | stating that You changed the files; and 100 | 101 | (c) You must retain, in the Source form of any Derivative Works 102 | that You distribute, all copyright, patent, trademark, and 103 | attribution notices from the Source form of the Work, 104 | excluding those notices that do not pertain to any part of 105 | the Derivative Works; and 106 | 107 | (d) If the Work includes a "NOTICE" text file as part of its 108 | distribution, then any Derivative Works that You distribute must 109 | include a readable copy of the attribution notices contained 110 | within such NOTICE file, excluding those notices that do not 111 | pertain to any part of the Derivative Works, in at least one 112 | of the following places: within a NOTICE text file distributed 113 | as part of the Derivative Works; within the Source form or 114 | documentation, if provided along with the Derivative Works; or, 115 | within a display generated by the Derivative Works, if and 116 | wherever such third-party notices normally appear. The contents 117 | of the NOTICE file are for informational purposes only and 118 | do not modify the License. You may add Your own attribution 119 | notices within Derivative Works that You distribute, alongside 120 | or as an addendum to the NOTICE text from the Work, provided 121 | that such additional attribution notices cannot be construed 122 | as modifying the License. 123 | 124 | You may add Your own copyright statement to Your modifications and 125 | may provide additional or different license terms and conditions 126 | for use, reproduction, or distribution of Your modifications, or 127 | for any such Derivative Works as a whole, provided Your use, 128 | reproduction, and distribution of the Work otherwise complies with 129 | the conditions stated in this License. 130 | 131 | 5. Submission of Contributions. Unless You explicitly state otherwise, 132 | any Contribution intentionally submitted for inclusion in the Work 133 | by You to the Licensor shall be under the terms and conditions of 134 | this License, without any additional terms or conditions. 135 | Notwithstanding the above, nothing herein shall supersede or modify 136 | the terms of any separate license agreement you may have executed 137 | with Licensor regarding such Contributions. 138 | 139 | 6. Trademarks. This License does not grant permission to use the trade 140 | names, trademarks, service marks, or product names of the Licensor, 141 | except as required for reasonable and customary use in describing the 142 | origin of the Work and reproducing the content of the NOTICE file. 143 | 144 | 7. Disclaimer of Warranty. Unless required by applicable law or 145 | agreed to in writing, Licensor provides the Work (and each 146 | Contributor provides its Contributions) on an "AS IS" BASIS, 147 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or 148 | implied, including, without limitation, any warranties or conditions 149 | of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A 150 | PARTICULAR PURPOSE. You are solely responsible for determining the 151 | appropriateness of using or redistributing the Work and assume any 152 | risks associated with Your exercise of permissions under this License. 153 | 154 | 8. Limitation of Liability. In no event and under no legal theory, 155 | whether in tort (including negligence), contract, or otherwise, 156 | unless required by applicable law (such as deliberate and grossly 157 | negligent acts) or agreed to in writing, shall any Contributor be 158 | liable to You for damages, including any direct, indirect, special, 159 | incidental, or consequential damages of any character arising as a 160 | result of this License or out of the use or inability to use the 161 | Work (including but not limited to damages for loss of goodwill, 162 | work stoppage, computer failure or malfunction, or any and all 163 | other commercial damages or losses), even if such Contributor 164 | has been advised of the possibility of such damages. 165 | 166 | 9. Accepting Warranty or Additional Liability. While redistributing 167 | the Work or Derivative Works thereof, You may choose to offer, 168 | and charge a fee for, acceptance of support, warranty, indemnity, 169 | or other liability obligations and/or rights consistent with this 170 | License. However, in accepting such obligations, You may act only 171 | on Your own behalf and on Your sole responsibility, not on behalf 172 | of any other Contributor, and only if You agree to indemnify, 173 | defend, and hold each Contributor harmless for any liability 174 | incurred by, or claims asserted against, such Contributor by reason 175 | of your accepting any such warranty or additional liability. 176 | 177 | END OF TERMS AND CONDITIONS 178 | 179 | APPENDIX: How to apply the Apache License to your work. 180 | 181 | To apply the Apache License to your work, attach the following 182 | boilerplate notice, with the fields enclosed by brackets "[]" 183 | replaced with your own identifying information. (Don't include 184 | the brackets!) The text should be enclosed in the appropriate 185 | comment syntax for the file format. We also recommend that a 186 | file or class name and description of purpose be included on the 187 | same "printed page" as the copyright notice for easier 188 | identification within third-party archives. 189 | 190 | Copyright [yyyy] [name of copyright owner] 191 | 192 | Licensed under the Apache License, Version 2.0 (the "License"); 193 | you may not use this file except in compliance with the License. 194 | You may obtain a copy of the License at 195 | 196 | http://www.apache.org/licenses/LICENSE-2.0 197 | 198 | Unless required by applicable law or agreed to in writing, software 199 | distributed under the License is distributed on an "AS IS" BASIS, 200 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 201 | See the License for the specific language governing permissions and 202 | limitations under the License. 203 | --------------------------------------------------------------------------------