Skip to content

Commit

Permalink
expose rest url in sys.nodes table
Browse files Browse the repository at this point in the history
  • Loading branch information
chaudum committed Apr 23, 2015
1 parent 0693365 commit 934600b
Show file tree
Hide file tree
Showing 9 changed files with 85 additions and 6 deletions.
2 changes: 2 additions & 0 deletions CHANGES.txt
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@ Changes for Crate
Unreleased
==========

- Expose REST URL of node in ``sys.nodes`` table

- Fix: In some cases too few threads where started which could slow
down queries

Expand Down
4 changes: 2 additions & 2 deletions docs/clients/client.txt
Original file line number Diff line number Diff line change
Expand Up @@ -38,8 +38,8 @@ Retrieving the returned columns using the ``cols()`` method::

will print::

["id", "name", "hostname", "port", "load", "mem", "heap", "fs", "version",
"thread_pools"]
["id", "name", "hostname", "rest_url", "port", "load", "mem", "heap",
"fs", "version", "thread_pools"]

Retrieving the returned rows using the ``rows()`` method::

Expand Down
11 changes: 11 additions & 0 deletions docs/sql/system.txt
Original file line number Diff line number Diff line change
Expand Up @@ -182,6 +182,17 @@ hostname
| | is running on. | |
+--------------+-------------------------------------------------+-------------+

rest_url
--------

+--------------+-----------------------------------------------------+-------------+
| Column Name | Description | Return Type |
+==============+=====================================================+=============+
| ``rest_url`` | Full http(s) address where the REST API of the node | ``String`` |
| | is exposed, including schema, hostname (or IP) | |
| | and port. | |
+--------------+-----------------------------------------------------+-------------+

port
----

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,7 @@ public class SysNodesTableInfo extends SysTableInfo {
register("id", DataTypes.STRING, null);
register("name", DataTypes.STRING, null);
register("hostname", DataTypes.STRING, null);
register("rest_url", DataTypes.STRING, null);
register("port", DataTypes.OBJECT, null);
register("port", DataTypes.INTEGER, ImmutableList.of("http"));
register("port", DataTypes.INTEGER, ImmutableList.of("transport"));
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
/*
* Licensed to CRATE Technology GmbH ("Crate") under one or more contributor
* license agreements. See the NOTICE file distributed with this work for
* additional information regarding copyright ownership. Crate licenses
* this file to you under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License. You may
* obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
* WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
* License for the specific language governing permissions and limitations
* under the License.
*
* However, if you have executed another commercial license agreement
* with Crate these terms will supersede the license and you may use the
* software solely pursuant to the terms of the relevant commercial agreement.
*/

package io.crate.operation.reference.sys.node;

import org.apache.lucene.util.BytesRef;
import org.elasticsearch.cluster.node.DiscoveryNodeService;
import org.elasticsearch.common.inject.Inject;

public class NodeRestUrlExpression extends SysNodeExpression<BytesRef> {

public static final String NAME = "rest_url";

private final DiscoveryNodeService discoveryNodeService;

@Inject
public NodeRestUrlExpression(DiscoveryNodeService discoveryNodeService) {
this.discoveryNodeService = discoveryNodeService;
}

@Override
public BytesRef value() {
String val = discoveryNodeService.buildAttributes().get("http_address");
if (val != null) {
return new BytesRef(val);
}
return new BytesRef();
}

}

Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,7 @@ protected void configure() {

bindExpr(NodeFsExpression.NAME, NodeFsExpression.class);
bindExpr(NodeHostnameExpression.NAME, NodeHostnameExpression.class);
bindExpr(NodeRestUrlExpression.NAME, NodeRestUrlExpression.class);
bindExpr(NodeIdExpression.NAME, NodeIdExpression.class);
bindExpr(NodeLoadExpression.NAME, NodeLoadExpression.class);
bindExpr(NodeMemoryExpression.NAME, NodeMemoryExpression.class);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -272,8 +272,8 @@ public void testAllColumnNodes() throws Exception {
List<String> outputNames = outputNames(analysis.relation());
assertThat(outputNames.get(0), is("id"));
assertThat(outputNames.get(1), is("id"));
assertThat(outputNames.size(), is(14));
assertThat(analysis.relation().querySpec().outputs().size(), is(14));
assertThat(outputNames.size(), is(15));
assertThat(analysis.relation().querySpec().outputs().size(), is(15));
}

@Test
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -431,7 +431,7 @@ public void testTableConstraintsWithOrderBy() throws Exception {
@Test
public void testDefaultColumns() throws Exception {
execute("select * from information_schema.columns order by schema_name, table_name");
assertEquals(203L, response.rowCount());
assertEquals(204L, response.rowCount());
}

@Test
Expand Down Expand Up @@ -551,7 +551,7 @@ public void testGlobalAggregation() throws Exception {
execute("select max(ordinal_position) from information_schema.columns");
assertEquals(1, response.rowCount());

short max_ordinal = 78;
short max_ordinal = 79;
assertEquals(max_ordinal, response.rows()[0][0]);

execute("create table t1 (id integer, col1 string)");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
package io.crate.operation.reference.sys;

import com.google.common.collect.ImmutableList;
import com.google.common.collect.ImmutableMap;
import io.crate.Build;
import io.crate.Version;
import io.crate.metadata.GlobalReferenceResolver;
Expand All @@ -40,6 +41,7 @@
import org.elasticsearch.action.admin.cluster.node.stats.NodeStats;
import org.elasticsearch.cluster.ClusterService;
import org.elasticsearch.cluster.node.DiscoveryNode;
import org.elasticsearch.cluster.node.DiscoveryNodeService;
import org.elasticsearch.common.inject.AbstractModule;
import org.elasticsearch.common.inject.Injector;
import org.elasticsearch.common.inject.ModulesBuilder;
Expand Down Expand Up @@ -148,6 +150,12 @@ public double[] answer(InvocationOnMock invocation) throws Throwable {
when(nodeService.stats()).thenReturn(nodeStats);
when(nodeStats.getHostname()).thenReturn("localhost");

DiscoveryNodeService discoveryNodeService = mock(DiscoveryNodeService.class);
when(discoveryNodeService.buildAttributes()).thenReturn(
ImmutableMap.<String, String>builder().put("http_address", "http://localhost:44200").build()
);
bind(DiscoveryNodeService.class).toInstance(discoveryNodeService);

DiscoveryNode node = mock(DiscoveryNode.class);
when(nodeStats.getNode()).thenReturn(node);

Expand Down Expand Up @@ -352,6 +360,13 @@ public void testHostname() throws Exception {
assertEquals(new BytesRef("localhost"), hostname.value());
}

@Test
public void testRestUrl() throws Exception {
ReferenceIdent ident = new ReferenceIdent(SysNodesTableInfo.IDENT, "rest_url");
SysExpression<BytesRef> http_addr = (SysExpression<BytesRef>) resolver.getImplementation(ident);
assertEquals(new BytesRef("http://localhost:44200"), http_addr.value());
}

@Test
public void testPorts() throws Exception {

Expand Down

0 comments on commit 934600b

Please sign in to comment.