Skip to content

Commit

Permalink
Fix JMX connector
Browse files Browse the repository at this point in the history
This fixes two problems:

* Calling toString() the Optional ObjectName
* Using toMap() which does not support null values
  • Loading branch information
electrum committed Jul 24, 2015
1 parent 5821c74 commit 5d2d290
Show file tree
Hide file tree
Showing 4 changed files with 60 additions and 5 deletions.
1 change: 1 addition & 0 deletions presto-docs/src/main/sphinx/release/release-0.113.rst
Original file line number Diff line number Diff line change
Expand Up @@ -17,3 +17,4 @@ General Changes
---------------

* Add :func:`element_at` function.
* Fix JMX connector. In the previous release it always returned zero rows.
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,7 @@ public JmxTableHandle getTableHandle(ConnectorSession session, SchemaTableName t
}
columns.add(new JmxColumnHandle(connectorId, attribute.getName(), getColumnType(attribute)));
}
return new JmxTableHandle(connectorId, objectName.toString(), columns.build());
return new JmxTableHandle(connectorId, objectName.get().toString(), columns.build());
}
catch (JMException e) {
return null;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@

import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;
Expand All @@ -38,7 +39,6 @@
import static com.facebook.presto.connector.jmx.Types.checkType;
import static com.google.common.base.Preconditions.checkArgument;
import static java.util.Objects.requireNonNull;
import static java.util.stream.Collectors.toMap;

public class JmxRecordSetProvider
implements ConnectorRecordSetProvider
Expand Down Expand Up @@ -166,8 +166,10 @@ private Map<String, Object> getAttributes(Set<String> uniqueColumnNames, JmxTabl

String[] columnNamesArray = uniqueColumnNames.toArray(new String[uniqueColumnNames.size()]);

return mbeanServer.getAttributes(objectName, columnNamesArray)
.asList().stream()
.collect(toMap(Attribute::getName, Attribute::getValue));
Map<String, Object> map = new HashMap<>();
for (Attribute attribute : mbeanServer.getAttributes(objectName, columnNamesArray).asList()) {
map.put(attribute.getName(), attribute.getValue());
}
return map;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
package com.facebook.presto.connector.jmx;

import com.facebook.presto.spi.ConnectorSession;
import com.facebook.presto.spi.SchemaTableName;
import com.facebook.presto.spi.type.BigintType;
import com.google.common.collect.ImmutableList;
import org.testng.annotations.Test;

import java.util.List;

import static com.facebook.presto.spi.type.TimeZoneKey.UTC_KEY;
import static com.facebook.presto.spi.type.VarcharType.VARCHAR;
import static java.lang.management.ManagementFactory.getPlatformMBeanServer;
import static java.util.Locale.ENGLISH;
import static org.testng.Assert.assertEquals;
import static org.testng.Assert.assertTrue;

public class TestJmxMetadata
{
private static final ConnectorSession SESSION = new ConnectorSession("user", UTC_KEY, ENGLISH, System.currentTimeMillis(), null);
private static final String RUNTIME_OBJECT = "java.lang:type=Runtime";
private static final SchemaTableName RUNTIME_TABLE = new SchemaTableName("jmx", RUNTIME_OBJECT.toLowerCase(ENGLISH));

private final JmxMetadata metadata = new JmxMetadata("test", getPlatformMBeanServer());

@Test
public void testListSchemas()
throws Exception
{
assertEquals(metadata.listSchemaNames(SESSION), ImmutableList.of("jmx"));
}

@Test
public void testListTables()
{
assertTrue(metadata.listTables(SESSION, "jmx").contains(RUNTIME_TABLE));
}

@Test
public void testGetTableHandle()
throws Exception
{
JmxTableHandle handle = metadata.getTableHandle(SESSION, RUNTIME_TABLE);
assertEquals(handle.getConnectorId(), "test");
assertEquals(handle.getObjectName(), RUNTIME_OBJECT);

List<JmxColumnHandle> columns = handle.getColumns();
assertTrue(columns.contains(new JmxColumnHandle("test", "node", VARCHAR)));
assertTrue(columns.contains(new JmxColumnHandle("test", "Name", VARCHAR)));
assertTrue(columns.contains(new JmxColumnHandle("test", "StartTime", BigintType.BIGINT)));
}
}

0 comments on commit 5d2d290

Please sign in to comment.