This is an automated email from the ASF dual-hosted git repository.

mck pushed a commit to branch trunk
in repository https://gitbox.apache.org/repos/asf/cassandra.git


The following commit(s) were added to refs/heads/trunk by this push:
     new 107dcc7  Add JAVA_HOME and java.rmi.server.randomID to the 
system_properties table
107dcc7 is described below

commit 107dcc766fbb16037295995ef87053e53a675a79
Author: Ekaterina Dimitrova <[email protected]>
AuthorDate: Mon Mar 16 09:46:01 2020 -0400

    Add JAVA_HOME and java.rmi.server.randomID to the system_properties table
    
     patch by Ekaterina Dimitrova; reviewed by Mick Semb Wever for 
CASSANDRA-15643
---
 CHANGES.txt                                        |   2 +-
 .../db/virtual/SystemPropertiesTable.java          |  19 +++-
 .../db/virtual/SystemPropertiesTableTest.java      | 115 +++++++++++++++++++--
 3 files changed, 126 insertions(+), 10 deletions(-)

diff --git a/CHANGES.txt b/CHANGES.txt
index c6a2eac..6a1cf93 100644
--- a/CHANGES.txt
+++ b/CHANGES.txt
@@ -1,7 +1,7 @@
 4.0-alpha4
  * Add compaction allocation measurement test (CASSANDRA-15388)
  * Added UnleveledSSTables global and table level metric (CASSANDRA-15620)
- * Added Virtual Table exposing Cassandra relevant system properties 
(CASSANDRA-15616)
+ * Added Virtual Table exposing Cassandra relevant system properties 
(CASSANDRA-15616, CASSANDRA-15643)
  * Add data modeling introduction (CASSANDRA-15481)
  * Improve the algorithmic token allocation in case racks = RF 
(CASSANDRA-15600)
  * Fix ConnectionTest.testAcquireReleaseOutbound (CASSANDRA-15308)
diff --git 
a/src/java/org/apache/cassandra/db/virtual/SystemPropertiesTable.java 
b/src/java/org/apache/cassandra/db/virtual/SystemPropertiesTable.java
index 864b11b..e8c13e7 100644
--- a/src/java/org/apache/cassandra/db/virtual/SystemPropertiesTable.java
+++ b/src/java/org/apache/cassandra/db/virtual/SystemPropertiesTable.java
@@ -47,6 +47,7 @@ final class SystemPropertiesTable extends AbstractVirtualTable
             "sun.arch.data.model",
             // jmx properties
             "java.rmi.server.hostname",
+            "java.rmi.server.randomID",
             "com.sun.management.jmxremote.authenticate",
             "com.sun.management.jmxremote.rmi.port",
             "com.sun.management.jmxremote.ssl",
@@ -62,7 +63,13 @@ final class SystemPropertiesTable extends 
AbstractVirtualTable
             "cassandra-foreground",
             "cassandra-pidfile",
             "default.provide.overlapping.tombstones",
-            "org.apache.cassandra.disable_mbean_registration"
+            "org.apache.cassandra.disable_mbean_registration",
+            // only for testing
+            "org.apache.cassandra.db.virtual.SystemPropertiesTableTest"
+            );
+
+    private static final Set<String> CASSANDRA_RELEVANT_ENVS = Sets.newHashSet(
+            "JAVA_HOME"
             );
 
     SystemPropertiesTable(String keyspace)
@@ -80,6 +87,11 @@ final class SystemPropertiesTable extends 
AbstractVirtualTable
     {
         SimpleDataSet result = new SimpleDataSet(metadata());
 
+        System.getenv().keySet()
+                .stream()
+                .filter(SystemPropertiesTable::isCassandraRelevant)
+                .forEach(name -> addRow(result, name, System.getenv(name)));
+
         System.getProperties().stringPropertyNames()
                 .stream()
                 .filter(SystemPropertiesTable::isCassandraRelevant)
@@ -94,14 +106,15 @@ final class SystemPropertiesTable extends 
AbstractVirtualTable
         SimpleDataSet result = new SimpleDataSet(metadata());
         String name = UTF8Type.instance.compose(partitionKey.getKey());
         if (isCassandraRelevant(name))
-            addRow(result, name, System.getProperty(name));
+            addRow(result, name, System.getProperty(name, 
System.getenv(name)));
 
         return result;
     }
 
     static boolean isCassandraRelevant(String name)
     {
-        return name.startsWith(Config.PROPERTY_PREFIX) || 
CASSANDRA_RELEVANT_PROPERTIES.contains(name);
+        return name.startsWith(Config.PROPERTY_PREFIX) || 
CASSANDRA_RELEVANT_PROPERTIES.contains(name)
+                                                       || 
CASSANDRA_RELEVANT_ENVS.contains(name);
     }
 
     private static void addRow(SimpleDataSet result, String name, String value)
diff --git 
a/test/unit/org/apache/cassandra/db/virtual/SystemPropertiesTableTest.java 
b/test/unit/org/apache/cassandra/db/virtual/SystemPropertiesTableTest.java
index ef952e8..2ec0683 100644
--- a/test/unit/org/apache/cassandra/db/virtual/SystemPropertiesTableTest.java
+++ b/test/unit/org/apache/cassandra/db/virtual/SystemPropertiesTableTest.java
@@ -18,10 +18,13 @@
 
 package org.apache.cassandra.db.virtual;
 
+import java.lang.reflect.Field;
+import java.util.Collections;
 import java.util.List;
+import java.util.Map;
 import java.util.stream.Collectors;
+import java.util.stream.Stream;
 
-import com.google.common.collect.ImmutableList;
 import org.junit.Assert;
 import org.junit.Before;
 import org.junit.BeforeClass;
@@ -31,9 +34,14 @@ import com.datastax.driver.core.ResultSet;
 import com.datastax.driver.core.Row;
 import org.apache.cassandra.cql3.CQLTester;
 
+import com.google.common.collect.ImmutableList;
+import com.google.common.collect.Maps;
+
 public class SystemPropertiesTableTest extends CQLTester
 {
     private static final String KS_NAME = "vts";
+    private static final Map<String, String> ORIGINAL_ENV_MAP = 
System.getenv();
+    private static final String TEST_PROP = 
"org.apache.cassandra.db.virtual.SystemPropertiesTableTest";
 
     private SystemPropertiesTable table;
 
@@ -56,22 +64,21 @@ public class SystemPropertiesTableTest extends CQLTester
         ResultSet result = executeNet("SELECT * FROM vts.system_properties");
 
         for (Row r : result)
-            Assert.assertEquals(System.getProperty(r.getString("name")), 
r.getString("value"));
+            Assert.assertEquals(System.getProperty(r.getString("name"), 
System.getenv(r.getString("name"))), r.getString("value"));
     }
 
     @Test
     public void testSelectPartition() throws Throwable
     {
-        List<String> properties = System.getProperties()
-                                        .stringPropertyNames()
-                                        .stream()
+        List<String> properties = 
Stream.concat(System.getProperties().stringPropertyNames().stream(),
+                                                
System.getenv().keySet().stream())
                                         .filter(name -> 
SystemPropertiesTable.isCassandraRelevant(name))
                                         .collect(Collectors.toList());
 
         for (String property : properties)
         {
             String q = "SELECT * FROM vts.system_properties WHERE name = '" + 
property + '\'';
-            assertRowsNet(executeNet(q), new Object[] {property, 
System.getProperty(property)});
+            assertRowsNet(executeNet(q), new Object[] {property, 
System.getProperty(property, System.getenv(property))});
         }
     }
 
@@ -82,4 +89,100 @@ public class SystemPropertiesTableTest extends CQLTester
         assertRowsNet(executeNet(q));
     }
 
+    @Test
+    public void testSelectProperty() throws Throwable
+    {
+        try
+        {
+            String value = "test_value";
+            System.setProperty(TEST_PROP, value);
+            String q = String.format("SELECT * FROM vts.system_properties 
WHERE name = '%s'", TEST_PROP);
+            assertRowsNet(executeNet(q), new Object[] {TEST_PROP, value});
+        }
+        finally
+        {
+            System.clearProperty(TEST_PROP);
+        }
+    }
+
+    @Test
+    public void testSelectEnv() throws Throwable
+    {
+        try
+        {
+            String value = "test_value";
+            addEnv(TEST_PROP, value);
+            String q = String.format("SELECT * FROM vts.system_properties 
WHERE name = '%s'", TEST_PROP);
+            assertRowsNet(executeNet(q), new Object[] {TEST_PROP, value});
+        }
+        finally
+        {
+            resetEnv();
+        }
+    }
+
+    @Test
+    public void testSelectPropertyOverEnv() throws Throwable
+    {
+        try
+        {
+            String value = "test_value";
+            System.setProperty(TEST_PROP, value);
+            addEnv(TEST_PROP, "wrong_value");
+            String q = String.format("SELECT * FROM vts.system_properties 
WHERE name = '%s'", TEST_PROP);
+            assertRowsNet(executeNet(q), new Object[] {TEST_PROP, value});
+        }
+        finally
+        {
+            System.clearProperty(TEST_PROP);
+            resetEnv();
+        }
+    }
+
+    private static void addEnv(String env, String value) throws 
ReflectiveOperationException
+    {
+        Map<String, String> envMap = Maps.newConcurrentMap();
+        envMap.putAll(System.getenv());
+        envMap.put(env, value);
+        setEnv(envMap);
+    }
+
+    private static void resetEnv() throws ReflectiveOperationException
+    {
+        setEnv(ORIGINAL_ENV_MAP);
+    }
+
+    private static void setEnv(Map<String, String> newenv) throws 
ReflectiveOperationException
+    {
+        try
+        {
+            Class<?> cls = Class.forName("java.lang.ProcessEnvironment");
+            Field field = cls.getDeclaredField("theEnvironment");
+            field.setAccessible(true);
+            Map<String, String> envMap = (Map<String, String>) field.get(null);
+            envMap.clear();
+            envMap.putAll(newenv);
+            field = cls.getDeclaredField("theCaseInsensitiveEnvironment");
+            field.setAccessible(true);
+            envMap = (Map<String, String>) field.get(null);
+            envMap.clear();
+            envMap.putAll(newenv);
+        }
+        catch (NoSuchFieldException ignore)
+        {
+            Class[] classes = Collections.class.getDeclaredClasses();
+            Map<String, String> envMap = System.getenv();
+            for(Class cl : classes) {
+                
if("java.util.Collections$UnmodifiableMap".equals(cl.getName()))
+                {
+                    Field field = cl.getDeclaredField("m");
+                    field.setAccessible(true);
+                    Object obj = field.get(envMap);
+                    envMap = (Map<String, String>) obj;
+                    envMap.clear();
+                    envMap.putAll(newenv);
+                }
+            }
+        }
+    }
 }


---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]

Reply via email to