You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@phoenix.apache.org by je...@apache.org on 2014/03/05 23:55:15 UTC
[29/50] [abbrv] git commit: Fix broken unit tests
Fix broken unit tests
Project: http://git-wip-us.apache.org/repos/asf/incubator-phoenix/repo
Commit: http://git-wip-us.apache.org/repos/asf/incubator-phoenix/commit/0237b9e7
Tree: http://git-wip-us.apache.org/repos/asf/incubator-phoenix/tree/0237b9e7
Diff: http://git-wip-us.apache.org/repos/asf/incubator-phoenix/diff/0237b9e7
Branch: refs/heads/4.0
Commit: 0237b9e73f7ab01d95c6933eee3e19ac28414a15
Parents: 6462137
Author: James Taylor <ja...@apache.org>
Authored: Tue Mar 4 14:22:11 2014 -0800
Committer: James Taylor <ja...@apache.org>
Committed: Tue Mar 4 14:22:11 2014 -0800
----------------------------------------------------------------------
.../apache/phoenix/compile/OrderByCompiler.java | 11 +-
.../query/ConnectionQueryServicesImpl.java | 1 +
.../query/ConnectionlessQueryServicesImpl.java | 4 +
.../org/apache/phoenix/query/StatsManager.java | 2 +
.../apache/phoenix/query/StatsManagerImpl.java | 6 +-
.../phoenix/compile/QueryCompilerTest.java | 44 ++--
...BaseParallelIteratorsRegionSplitterTest.java | 92 ++++++++
...aultParallelIteratorsRegionSplitterTest.java | 217 +------------------
.../phoenix/end2end/StatsManagerTest.java | 195 +++++++++++++++++
9 files changed, 327 insertions(+), 245 deletions(-)
----------------------------------------------------------------------
http://git-wip-us.apache.org/repos/asf/incubator-phoenix/blob/0237b9e7/phoenix-core/src/main/java/org/apache/phoenix/compile/OrderByCompiler.java
----------------------------------------------------------------------
diff --git a/phoenix-core/src/main/java/org/apache/phoenix/compile/OrderByCompiler.java b/phoenix-core/src/main/java/org/apache/phoenix/compile/OrderByCompiler.java
index af7000b..df9ce2c 100644
--- a/phoenix-core/src/main/java/org/apache/phoenix/compile/OrderByCompiler.java
+++ b/phoenix-core/src/main/java/org/apache/phoenix/compile/OrderByCompiler.java
@@ -23,9 +23,6 @@ import java.util.Collections;
import java.util.LinkedHashSet;
import java.util.List;
-import com.google.common.collect.ImmutableList;
-import com.google.common.collect.Lists;
-import com.google.common.collect.Sets;
import org.apache.phoenix.compile.GroupByCompiler.GroupBy;
import org.apache.phoenix.compile.TrackOrderPreservingExpressionCompiler.Ordering;
import org.apache.phoenix.exception.SQLExceptionCode;
@@ -37,6 +34,10 @@ import org.apache.phoenix.parse.OrderByNode;
import org.apache.phoenix.query.ConnectionQueryServices.Feature;
import org.apache.phoenix.schema.SortOrder;
+import com.google.common.collect.ImmutableList;
+import com.google.common.collect.Lists;
+import com.google.common.collect.Sets;
+
/**
* Validates ORDER BY clause and builds up a list of referenced columns.
*
@@ -100,10 +101,6 @@ public class OrderByCompiler {
ExpressionCompiler.throwNonAggExpressionInAggException(expression.toString());
}
}
- if (expression.getDataType().isArrayType()) {
- throw new SQLExceptionInfo.Builder(SQLExceptionCode.ORDER_BY_ARRAY_NOT_SUPPORTED)
- .setMessage(expression.toString()).build().buildException();
- }
if (expression.getSortOrder() == SortOrder.DESC) {
isAscending = !isAscending;
}
http://git-wip-us.apache.org/repos/asf/incubator-phoenix/blob/0237b9e7/phoenix-core/src/main/java/org/apache/phoenix/query/ConnectionQueryServicesImpl.java
----------------------------------------------------------------------
diff --git a/phoenix-core/src/main/java/org/apache/phoenix/query/ConnectionQueryServicesImpl.java b/phoenix-core/src/main/java/org/apache/phoenix/query/ConnectionQueryServicesImpl.java
index 7ad4a82..085bace 100644
--- a/phoenix-core/src/main/java/org/apache/phoenix/query/ConnectionQueryServicesImpl.java
+++ b/phoenix-core/src/main/java/org/apache/phoenix/query/ConnectionQueryServicesImpl.java
@@ -283,6 +283,7 @@ public class ConnectionQueryServicesImpl extends DelegateQueryServices implement
// Should not be necessary, but at test time in particular
// there seems to be a memory leak and this can't hurt.
childServices.clear();
+ statsManager.clearStats();
latestMetaData = null;
}
} catch (SQLException e) {
http://git-wip-us.apache.org/repos/asf/incubator-phoenix/blob/0237b9e7/phoenix-core/src/main/java/org/apache/phoenix/query/ConnectionlessQueryServicesImpl.java
----------------------------------------------------------------------
diff --git a/phoenix-core/src/main/java/org/apache/phoenix/query/ConnectionlessQueryServicesImpl.java b/phoenix-core/src/main/java/org/apache/phoenix/query/ConnectionlessQueryServicesImpl.java
index ea53149..56d1d1d 100644
--- a/phoenix-core/src/main/java/org/apache/phoenix/query/ConnectionlessQueryServicesImpl.java
+++ b/phoenix-core/src/main/java/org/apache/phoenix/query/ConnectionlessQueryServicesImpl.java
@@ -125,6 +125,10 @@ public class ConnectionlessQueryServicesImpl extends DelegateQueryServices imple
@Override
public void updateStats(TableRef table) throws SQLException {
}
+
+ @Override
+ public void clearStats() throws SQLException {
+ }
};
}
http://git-wip-us.apache.org/repos/asf/incubator-phoenix/blob/0237b9e7/phoenix-core/src/main/java/org/apache/phoenix/query/StatsManager.java
----------------------------------------------------------------------
diff --git a/phoenix-core/src/main/java/org/apache/phoenix/query/StatsManager.java b/phoenix-core/src/main/java/org/apache/phoenix/query/StatsManager.java
index f631ba1..df55fb5 100644
--- a/phoenix-core/src/main/java/org/apache/phoenix/query/StatsManager.java
+++ b/phoenix-core/src/main/java/org/apache/phoenix/query/StatsManager.java
@@ -54,4 +54,6 @@ public interface StatsManager {
* @throws SQLException
*/
void updateStats(TableRef table) throws SQLException;
+
+ void clearStats() throws SQLException;
}
http://git-wip-us.apache.org/repos/asf/incubator-phoenix/blob/0237b9e7/phoenix-core/src/main/java/org/apache/phoenix/query/StatsManagerImpl.java
----------------------------------------------------------------------
diff --git a/phoenix-core/src/main/java/org/apache/phoenix/query/StatsManagerImpl.java b/phoenix-core/src/main/java/org/apache/phoenix/query/StatsManagerImpl.java
index 4daa616..caad8cc 100644
--- a/phoenix-core/src/main/java/org/apache/phoenix/query/StatsManagerImpl.java
+++ b/phoenix-core/src/main/java/org/apache/phoenix/query/StatsManagerImpl.java
@@ -30,7 +30,6 @@ import org.apache.hadoop.hbase.client.Result;
import org.apache.hadoop.hbase.client.ResultScanner;
import org.apache.hadoop.hbase.client.Scan;
import org.apache.hadoop.hbase.filter.KeyOnlyFilter;
-
import org.apache.phoenix.schema.TableRef;
import org.apache.phoenix.util.SchemaUtil;
import org.apache.phoenix.util.ServerUtil;
@@ -216,4 +215,9 @@ public class StatsManagerImpl implements StatsManager {
return initiatedTime;
}
}
+
+ @Override
+ public void clearStats() throws SQLException {
+ tableStatsMap.clear();
+ }
}
http://git-wip-us.apache.org/repos/asf/incubator-phoenix/blob/0237b9e7/phoenix-core/src/test/java/org/apache/phoenix/compile/QueryCompilerTest.java
----------------------------------------------------------------------
diff --git a/phoenix-core/src/test/java/org/apache/phoenix/compile/QueryCompilerTest.java b/phoenix-core/src/test/java/org/apache/phoenix/compile/QueryCompilerTest.java
index 61dcda7..c47888d 100644
--- a/phoenix-core/src/test/java/org/apache/phoenix/compile/QueryCompilerTest.java
+++ b/phoenix-core/src/test/java/org/apache/phoenix/compile/QueryCompilerTest.java
@@ -155,7 +155,7 @@ public class QueryCompilerTest extends BaseConnectionlessQueryTest {
}
@Test
- public void testVarBinaryInMultipartPK() throws Exception {
+ public void testVarBinaryNotLastInMultipartPK() throws Exception {
Connection conn = DriverManager.getConnection(getUrl());
// When the VARBINARY key is the last column, it is allowed.
String query = "CREATE TABLE foo (a_string varchar not null, b_string varchar not null, a_binary varbinary not null, " +
@@ -169,7 +169,28 @@ public class QueryCompilerTest extends BaseConnectionlessQueryTest {
statement.execute();
fail();
} catch (SQLException e) {
- assertTrue(e.getMessage(), e.getMessage().contains("ERROR 1005 (42J03): The VARBINARY type can only be used as the last part of a multi-part row key. columnName=FOO.A_BINARY"));
+ assertEquals(SQLExceptionCode.VARBINARY_IN_ROW_KEY.getErrorCode(), e.getErrorCode());
+ } finally {
+ conn.close();
+ }
+ }
+
+ @Test
+ public void testArrayNotLastInMultipartPK() throws Exception {
+ Connection conn = DriverManager.getConnection(getUrl());
+ // When the VARBINARY key is the last column, it is allowed.
+ String query = "CREATE TABLE foo (a_string varchar not null, b_string varchar not null, a_array varchar[] not null, " +
+ "col1 decimal, col2 decimal CONSTRAINT pk PRIMARY KEY (a_string, b_string, a_array))";
+ PreparedStatement statement = conn.prepareStatement(query);
+ statement.execute();
+ try {
+ // VARBINARY key is not allowed in the middle of the key.
+ query = "CREATE TABLE foo (a_array varchar[] not null, a_string varchar not null, col1 decimal, col2 decimal CONSTRAINT pk PRIMARY KEY (a_array, a_string))";
+ statement = conn.prepareStatement(query);
+ statement.execute();
+ fail();
+ } catch (SQLException e) {
+ assertEquals(SQLExceptionCode.VARBINARY_IN_ROW_KEY.getErrorCode(), e.getErrorCode());
} finally {
conn.close();
}
@@ -1225,25 +1246,6 @@ public class QueryCompilerTest extends BaseConnectionlessQueryTest {
}
@Test
- public void testInvalidArrayInQuery () throws Exception {
- Connection conn = DriverManager.getConnection(getUrl());
- conn.createStatement().execute("CREATE TABLE t (k VARCHAR PRIMARY KEY, a INTEGER[10], B INTEGER[10])");
- try {
- conn.createStatement().execute("SELECT * FROM t ORDER BY a");
- fail();
- } catch (SQLException e) {
- assertEquals(SQLExceptionCode.ORDER_BY_ARRAY_NOT_SUPPORTED.getErrorCode(), e.getErrorCode());
- }
- try {
- conn.createStatement().execute("SELECT * FROM t WHERE a < b");
- fail();
- } catch (SQLException e) {
- assertEquals(SQLExceptionCode.NON_EQUALITY_ARRAY_COMPARISON.getErrorCode(), e.getErrorCode());
- }
- conn.close();
- }
-
- @Test
public void testInvalidArrayElemRefInUpsert() throws Exception {
Connection conn = DriverManager.getConnection(getUrl());
conn.createStatement().execute("CREATE TABLE t (k VARCHAR PRIMARY KEY, a INTEGER[10], B INTEGER[10])");
http://git-wip-us.apache.org/repos/asf/incubator-phoenix/blob/0237b9e7/phoenix-core/src/test/java/org/apache/phoenix/end2end/BaseParallelIteratorsRegionSplitterTest.java
----------------------------------------------------------------------
diff --git a/phoenix-core/src/test/java/org/apache/phoenix/end2end/BaseParallelIteratorsRegionSplitterTest.java b/phoenix-core/src/test/java/org/apache/phoenix/end2end/BaseParallelIteratorsRegionSplitterTest.java
new file mode 100644
index 0000000..8371e75
--- /dev/null
+++ b/phoenix-core/src/test/java/org/apache/phoenix/end2end/BaseParallelIteratorsRegionSplitterTest.java
@@ -0,0 +1,92 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements. See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership. The ASF 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.
+ */
+package org.apache.phoenix.end2end;
+
+import static org.apache.phoenix.util.TestUtil.STABLE_NAME;
+import static org.apache.phoenix.util.TestUtil.TEST_PROPERTIES;
+
+import java.sql.Connection;
+import java.sql.DriverManager;
+import java.sql.PreparedStatement;
+import java.sql.SQLException;
+import java.util.Map;
+import java.util.Properties;
+
+import org.apache.phoenix.jdbc.PhoenixConnection;
+import org.apache.phoenix.query.QueryServices;
+import org.apache.phoenix.schema.PTableKey;
+import org.apache.phoenix.schema.TableRef;
+import org.apache.phoenix.util.PhoenixRuntime;
+import org.apache.phoenix.util.ReadOnlyProps;
+import org.junit.BeforeClass;
+
+import com.google.common.collect.Maps;
+
+
+public class BaseParallelIteratorsRegionSplitterTest extends BaseClientManagedTimeTest {
+
+ protected static final byte[] KMIN = new byte[] {'!'};
+ protected static final byte[] KMIN2 = new byte[] {'.'};
+ protected static final byte[] K1 = new byte[] {'a'};
+ protected static final byte[] K3 = new byte[] {'c'};
+ protected static final byte[] K4 = new byte[] {'d'};
+ protected static final byte[] K5 = new byte[] {'e'};
+ protected static final byte[] K6 = new byte[] {'f'};
+ protected static final byte[] K9 = new byte[] {'i'};
+ protected static final byte[] K11 = new byte[] {'k'};
+ protected static final byte[] K12 = new byte[] {'l'};
+ protected static final byte[] KMAX = new byte[] {'~'};
+ protected static final byte[] KMAX2 = new byte[] {'z'};
+
+ @BeforeClass
+ public static void doSetup() throws Exception {
+ int targetQueryConcurrency = 3;
+ int maxQueryConcurrency = 5;
+ Map<String,String> props = Maps.newHashMapWithExpectedSize(3);
+ props.put(QueryServices.MAX_QUERY_CONCURRENCY_ATTRIB, Integer.toString(maxQueryConcurrency));
+ props.put(QueryServices.TARGET_QUERY_CONCURRENCY_ATTRIB, Integer.toString(targetQueryConcurrency));
+ props.put(QueryServices.MAX_INTRA_REGION_PARALLELIZATION_ATTRIB, Integer.toString(Integer.MAX_VALUE));
+ // Must update config before starting server
+ startServer(getUrl(), new ReadOnlyProps(props.entrySet().iterator()));
+ }
+
+ protected void initTableValues(long ts) throws Exception {
+ byte[][] splits = new byte[][] {K3,K4,K9,K11};
+ ensureTableCreated(getUrl(),STABLE_NAME,splits, ts-2);
+ String url = getUrl() + ";" + PhoenixRuntime.CURRENT_SCN_ATTRIB + "=" + ts;
+ Properties props = new Properties(TEST_PROPERTIES);
+ Connection conn = DriverManager.getConnection(url, props);
+ PreparedStatement stmt = conn.prepareStatement(
+ "upsert into " + STABLE_NAME + " VALUES (?, ?)");
+ stmt.setString(1, new String(KMIN));
+ stmt.setInt(2, 1);
+ stmt.execute();
+ stmt.setString(1, new String(KMAX));
+ stmt.setInt(2, 2);
+ stmt.execute();
+ conn.commit();
+ conn.close();
+ }
+
+ protected static TableRef getTableRef(Connection conn, long ts) throws SQLException {
+ PhoenixConnection pconn = conn.unwrap(PhoenixConnection.class);
+ TableRef table = new TableRef(null,pconn.getMetaDataCache().getTable(new PTableKey(pconn.getTenantId(), STABLE_NAME)),ts, false);
+ return table;
+ }
+
+}
http://git-wip-us.apache.org/repos/asf/incubator-phoenix/blob/0237b9e7/phoenix-core/src/test/java/org/apache/phoenix/end2end/DefaultParallelIteratorsRegionSplitterTest.java
----------------------------------------------------------------------
diff --git a/phoenix-core/src/test/java/org/apache/phoenix/end2end/DefaultParallelIteratorsRegionSplitterTest.java b/phoenix-core/src/test/java/org/apache/phoenix/end2end/DefaultParallelIteratorsRegionSplitterTest.java
index 2c88c0a..870fef3 100644
--- a/phoenix-core/src/test/java/org/apache/phoenix/end2end/DefaultParallelIteratorsRegionSplitterTest.java
+++ b/phoenix-core/src/test/java/org/apache/phoenix/end2end/DefaultParallelIteratorsRegionSplitterTest.java
@@ -17,22 +17,15 @@
*/
package org.apache.phoenix.end2end;
-import static org.apache.phoenix.util.TestUtil.PHOENIX_JDBC_URL;
-import static org.apache.phoenix.util.TestUtil.STABLE_NAME;
import static org.apache.phoenix.util.TestUtil.TEST_PROPERTIES;
-import static org.junit.Assert.assertArrayEquals;
import static org.junit.Assert.assertEquals;
-import static org.junit.Assert.assertFalse;
-import static org.junit.Assert.assertTrue;
import java.sql.Connection;
import java.sql.DriverManager;
-import java.sql.PreparedStatement;
import java.sql.SQLException;
import java.util.Collections;
import java.util.Comparator;
import java.util.List;
-import java.util.Map;
import java.util.Properties;
import org.apache.hadoop.hbase.HConstants;
@@ -46,21 +39,11 @@ import org.apache.phoenix.jdbc.PhoenixStatement;
import org.apache.phoenix.parse.HintNode;
import org.apache.phoenix.query.ConnectionQueryServices;
import org.apache.phoenix.query.KeyRange;
-import org.apache.phoenix.query.QueryServices;
-import org.apache.phoenix.query.StatsManager;
-import org.apache.phoenix.query.StatsManagerImpl;
-import org.apache.phoenix.query.StatsManagerImpl.TimeKeeper;
import org.apache.phoenix.schema.PDataType;
-import org.apache.phoenix.schema.PTableKey;
import org.apache.phoenix.schema.TableRef;
import org.apache.phoenix.util.PhoenixRuntime;
-import org.apache.phoenix.util.ReadOnlyProps;
-import org.junit.BeforeClass;
-import org.junit.Ignore;
import org.junit.Test;
-import com.google.common.collect.Maps;
-
/**
* Tests for {@link DefaultParallelIteratorRegionSplitter}.
@@ -68,59 +51,7 @@ import com.google.common.collect.Maps;
*
* @since 0.1
*/
-public class DefaultParallelIteratorsRegionSplitterTest extends BaseClientManagedTimeTest {
-
- private static final byte[] KMIN = new byte[] {'!'};
- private static final byte[] KMIN2 = new byte[] {'.'};
- private static final byte[] K1 = new byte[] {'a'};
- private static final byte[] K3 = new byte[] {'c'};
- private static final byte[] K4 = new byte[] {'d'};
- private static final byte[] K5 = new byte[] {'e'};
- private static final byte[] K6 = new byte[] {'f'};
- private static final byte[] K9 = new byte[] {'i'};
- private static final byte[] K11 = new byte[] {'k'};
- private static final byte[] K12 = new byte[] {'l'};
- private static final byte[] KMAX = new byte[] {'~'};
- private static final byte[] KMAX2 = new byte[] {'z'};
-
- @BeforeClass
- public static void doSetup() throws Exception {
- int targetQueryConcurrency = 3;
- int maxQueryConcurrency = 5;
- Map<String,String> props = Maps.newHashMapWithExpectedSize(4);
- props.put(QueryServices.MAX_QUERY_CONCURRENCY_ATTRIB, Integer.toString(maxQueryConcurrency));
- props.put(QueryServices.TARGET_QUERY_CONCURRENCY_ATTRIB, Integer.toString(targetQueryConcurrency));
- props.put(QueryServices.MAX_INTRA_REGION_PARALLELIZATION_ATTRIB, Integer.toString(Integer.MAX_VALUE));
- // Drop the HBase table metadata for this test
- // Didn't help fix flapper TODO: remove
- // props.put(QueryServices.DROP_METADATA_ATTRIB, Boolean.toString(true));
- // Must update config before starting server
- startServer(getUrl(), new ReadOnlyProps(props.entrySet().iterator()));
- }
-
- private void initTableValues(long ts) throws Exception {
- byte[][] splits = new byte[][] {K3,K4,K9,K11};
- ensureTableCreated(getUrl(),STABLE_NAME,splits, ts-2);
- String url = getUrl() + ";" + PhoenixRuntime.CURRENT_SCN_ATTRIB + "=" + ts;
- Properties props = new Properties(TEST_PROPERTIES);
- Connection conn = DriverManager.getConnection(url, props);
- PreparedStatement stmt = conn.prepareStatement(
- "upsert into " + STABLE_NAME + " VALUES (?, ?)");
- stmt.setString(1, new String(KMIN));
- stmt.setInt(2, 1);
- stmt.execute();
- stmt.setString(1, new String(KMAX));
- stmt.setInt(2, 2);
- stmt.execute();
- conn.commit();
- conn.close();
- }
-
- private static TableRef getTableRef(Connection conn, long ts) throws SQLException {
- PhoenixConnection pconn = conn.unwrap(PhoenixConnection.class);
- TableRef table = new TableRef(null,pconn.getMetaDataCache().getTable(new PTableKey(pconn.getTenantId(), STABLE_NAME)),ts, false);
- return table;
- }
+public class DefaultParallelIteratorsRegionSplitterTest extends BaseParallelIteratorsRegionSplitterTest {
private static List<KeyRange> getSplits(Connection conn, long ts, final Scan scan)
throws SQLException {
@@ -208,152 +139,6 @@ public class DefaultParallelIteratorsRegionSplitterTest extends BaseClientManage
assertEquals(newKeyRange(new byte[] {'M'}, K3), keyRanges.get(2));
}
- private static class ManualTimeKeeper implements TimeKeeper {
- private long currentTime = 0;
- @Override
- public long currentTimeMillis() {
- return currentTime;
- }
-
- public void setCurrentTimeMillis(long currentTime) {
- this.currentTime = currentTime;
- }
- }
-
- private static interface ChangeDetector {
- boolean isChanged();
- }
-
- private boolean waitForAsyncChange(ChangeDetector detector, long maxWaitTimeMs) throws Exception {
- long startTime = System.currentTimeMillis();
- do {
- if (detector.isChanged()) {
- return true;
- }
- try {
- Thread.sleep(500);
- } catch (InterruptedException e) {
- throw e;
- }
- } while (System.currentTimeMillis() - startTime < maxWaitTimeMs);
- return false;
- }
-
- private static class MinKeyChange implements ChangeDetector {
- private byte[] value;
- private StatsManager stats;
- private TableRef table;
-
- public MinKeyChange(StatsManager stats, TableRef table) {
- this.value = stats.getMinKey(table);
- this.stats = stats;
- this.table = table;
- }
- @Override
- public boolean isChanged() {
- return value != stats.getMinKey(table);
- }
- }
-
- private static class MaxKeyChange implements ChangeDetector {
- private byte[] value;
- private StatsManager stats;
- private TableRef table;
-
- public MaxKeyChange(StatsManager stats, TableRef table) {
- this.value = stats.getMaxKey(table);
- this.stats = stats;
- this.table = table;
- }
- @Override
- public boolean isChanged() {
- return value != stats.getMaxKey(table);
- }
- }
-
- @Ignore // FIXME
- @Test
- public void testStatsManagerImpl() throws Exception {
- long ts = nextTimestamp();
- initTableValues(ts);
- String url = getUrl() + ";" + PhoenixRuntime.CURRENT_SCN_ATTRIB + "=" + ts;
- Properties props = new Properties(TEST_PROPERTIES);
- Connection conn = DriverManager.getConnection(url, props);
- TableRef table = getTableRef(conn,ts);
-
- int updateFreq = 5;
- int maxAge = 10;
- int startTime = 100;
- long waitTime = 5000;
-
- ManualTimeKeeper timeKeeper = new ManualTimeKeeper();
- timeKeeper.setCurrentTimeMillis(startTime);
- ConnectionQueryServices services = driver.getConnectionQueryServices(getUrl(), TEST_PROPERTIES);
- StatsManager stats = new StatsManagerImpl(services, updateFreq, maxAge, timeKeeper);
- MinKeyChange minKeyChange = new MinKeyChange(stats, table);
- MaxKeyChange maxKeyChange = new MaxKeyChange(stats, table);
-
- byte[] minKey = stats.getMinKey(table);
- assertTrue(minKey == null);
- assertTrue(waitForAsyncChange(minKeyChange,waitTime));
- assertArrayEquals(KMIN, stats.getMinKey(table));
- assertArrayEquals(KMAX, stats.getMaxKey(table));
- minKeyChange = new MinKeyChange(stats, table);
-
- url = PHOENIX_JDBC_URL + ";" + PhoenixRuntime.CURRENT_SCN_ATTRIB + "=" + ts+2;
- props = new Properties(TEST_PROPERTIES);
- conn = DriverManager.getConnection(url, props);
- PreparedStatement delStmt = conn.prepareStatement("delete from " + STABLE_NAME + " where id=?");
- delStmt.setString(1, new String(KMIN));
- delStmt.execute();
- PreparedStatement upsertStmt = conn.prepareStatement("upsert into " + STABLE_NAME + " VALUES (?, ?)");
- upsertStmt.setString(1, new String(KMIN2));
- upsertStmt.setInt(2, 1);
- upsertStmt.execute();
- conn.commit();
-
- assertFalse(waitForAsyncChange(minKeyChange,waitTime)); // Stats won't change until they're attempted to be retrieved again
- timeKeeper.setCurrentTimeMillis(timeKeeper.currentTimeMillis() + updateFreq);
- minKeyChange = new MinKeyChange(stats, table); // Will kick off change, but will upate asynchronously
- assertArrayEquals(KMIN, minKeyChange.value);
- assertTrue(waitForAsyncChange(minKeyChange,waitTime));
- assertArrayEquals(KMIN2, stats.getMinKey(table));
- assertArrayEquals(KMAX, stats.getMaxKey(table));
- minKeyChange = new MinKeyChange(stats, table);
-
- timeKeeper.setCurrentTimeMillis(timeKeeper.currentTimeMillis() + maxAge);
- minKeyChange = new MinKeyChange(stats, table); // Will kick off change, but will upate asynchronously
- assertTrue(null == minKeyChange.value);
- assertTrue(waitForAsyncChange(minKeyChange,waitTime));
- assertArrayEquals(KMIN2, stats.getMinKey(table));
- assertArrayEquals(KMAX, stats.getMaxKey(table));
- maxKeyChange = new MaxKeyChange(stats, table);
-
- delStmt.setString(1, new String(KMAX));
- delStmt.execute();
- upsertStmt.setString(1, new String(KMAX2));
- upsertStmt.setInt(2, 1);
- upsertStmt.execute();
- conn.commit();
- conn.close();
-
- assertFalse(waitForAsyncChange(maxKeyChange,waitTime)); // Stats won't change until they're attempted to be retrieved again
- timeKeeper.setCurrentTimeMillis(timeKeeper.currentTimeMillis() + updateFreq);
- maxKeyChange = new MaxKeyChange(stats, table); // Will kick off change, but will upate asynchronously
- assertArrayEquals(KMAX, maxKeyChange.value);
- assertTrue(waitForAsyncChange(maxKeyChange,waitTime));
- assertArrayEquals(KMAX2, stats.getMaxKey(table));
- assertArrayEquals(KMIN2, stats.getMinKey(table));
- maxKeyChange = new MaxKeyChange(stats, table);
-
- timeKeeper.setCurrentTimeMillis(timeKeeper.currentTimeMillis() + maxAge);
- maxKeyChange = new MaxKeyChange(stats, table); // Will kick off change, but will upate asynchronously
- assertTrue(null == maxKeyChange.value);
- assertTrue(waitForAsyncChange(maxKeyChange,waitTime));
- assertArrayEquals(KMIN2, stats.getMinKey(table));
- assertArrayEquals(KMAX2, stats.getMaxKey(table));
- }
-
private static KeyRange newKeyRange(byte[] lowerRange, byte[] upperRange) {
return PDataType.CHAR.getKeyRange(lowerRange, true, upperRange, false);
}
http://git-wip-us.apache.org/repos/asf/incubator-phoenix/blob/0237b9e7/phoenix-core/src/test/java/org/apache/phoenix/end2end/StatsManagerTest.java
----------------------------------------------------------------------
diff --git a/phoenix-core/src/test/java/org/apache/phoenix/end2end/StatsManagerTest.java b/phoenix-core/src/test/java/org/apache/phoenix/end2end/StatsManagerTest.java
new file mode 100644
index 0000000..3564248
--- /dev/null
+++ b/phoenix-core/src/test/java/org/apache/phoenix/end2end/StatsManagerTest.java
@@ -0,0 +1,195 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements. See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership. The ASF 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.
+ */
+package org.apache.phoenix.end2end;
+
+import static org.apache.phoenix.util.TestUtil.PHOENIX_JDBC_URL;
+import static org.apache.phoenix.util.TestUtil.STABLE_NAME;
+import static org.apache.phoenix.util.TestUtil.TEST_PROPERTIES;
+import static org.junit.Assert.assertArrayEquals;
+import static org.junit.Assert.assertFalse;
+import static org.junit.Assert.assertTrue;
+
+import java.sql.Connection;
+import java.sql.DriverManager;
+import java.sql.PreparedStatement;
+import java.util.Properties;
+
+import org.apache.phoenix.query.ConnectionQueryServices;
+import org.apache.phoenix.query.StatsManager;
+import org.apache.phoenix.query.StatsManagerImpl;
+import org.apache.phoenix.query.StatsManagerImpl.TimeKeeper;
+import org.apache.phoenix.schema.TableRef;
+import org.apache.phoenix.util.PhoenixRuntime;
+import org.junit.Test;
+
+
+/**
+ *
+ * Test for stats manager, which is a client-side process that caches the
+ * first and last key of a given table. The {@link #testStatsManager()}
+ * test must be the only test here, as it relies on state that is only
+ * cleared between test runs.
+ *
+ */
+public class StatsManagerTest extends BaseParallelIteratorsRegionSplitterTest {
+
+ private static class ManualTimeKeeper implements TimeKeeper {
+ private long currentTime = 0;
+ @Override
+ public long currentTimeMillis() {
+ return currentTime;
+ }
+
+ public void setCurrentTimeMillis(long currentTime) {
+ this.currentTime = currentTime;
+ }
+ }
+
+ private static interface ChangeDetector {
+ boolean isChanged();
+ }
+
+ private boolean waitForAsyncChange(ChangeDetector detector, long maxWaitTimeMs) throws Exception {
+ long startTime = System.currentTimeMillis();
+ do {
+ if (detector.isChanged()) {
+ return true;
+ }
+ try {
+ Thread.sleep(500);
+ } catch (InterruptedException e) {
+ throw e;
+ }
+ } while (System.currentTimeMillis() - startTime < maxWaitTimeMs);
+ return false;
+ }
+
+ private static class MinKeyChange implements ChangeDetector {
+ private byte[] value;
+ private StatsManager stats;
+ private TableRef table;
+
+ public MinKeyChange(StatsManager stats, TableRef table) {
+ this.value = stats.getMinKey(table);
+ this.stats = stats;
+ this.table = table;
+ }
+ @Override
+ public boolean isChanged() {
+ return value != stats.getMinKey(table);
+ }
+ }
+
+ private static class MaxKeyChange implements ChangeDetector {
+ private byte[] value;
+ private StatsManager stats;
+ private TableRef table;
+
+ public MaxKeyChange(StatsManager stats, TableRef table) {
+ this.value = stats.getMaxKey(table);
+ this.stats = stats;
+ this.table = table;
+ }
+ @Override
+ public boolean isChanged() {
+ return value != stats.getMaxKey(table);
+ }
+ }
+
+ @Test
+ public void testStatsManager() throws Exception {
+ long ts = nextTimestamp();
+ initTableValues(ts);
+ String url = getUrl() + ";" + PhoenixRuntime.CURRENT_SCN_ATTRIB + "=" + ts;
+ Properties props = new Properties(TEST_PROPERTIES);
+ Connection conn = DriverManager.getConnection(url, props);
+ TableRef table = getTableRef(conn,ts);
+
+ int updateFreq = 5;
+ int maxAge = 10;
+ int startTime = 100;
+ long waitTime = 5000;
+
+ ManualTimeKeeper timeKeeper = new ManualTimeKeeper();
+ timeKeeper.setCurrentTimeMillis(startTime);
+ ConnectionQueryServices services = driver.getConnectionQueryServices(getUrl(), TEST_PROPERTIES);
+ StatsManager stats = new StatsManagerImpl(services, updateFreq, maxAge, timeKeeper);
+ MinKeyChange minKeyChange = new MinKeyChange(stats, table);
+ MaxKeyChange maxKeyChange = new MaxKeyChange(stats, table);
+
+ byte[] minKey = stats.getMinKey(table);
+ assertTrue(minKey == null);
+ assertTrue(waitForAsyncChange(minKeyChange,waitTime));
+ assertArrayEquals(KMIN, stats.getMinKey(table));
+ assertArrayEquals(KMAX, stats.getMaxKey(table));
+ minKeyChange = new MinKeyChange(stats, table);
+
+ url = PHOENIX_JDBC_URL + ";" + PhoenixRuntime.CURRENT_SCN_ATTRIB + "=" + ts+2;
+ props = new Properties(TEST_PROPERTIES);
+ conn = DriverManager.getConnection(url, props);
+ PreparedStatement delStmt = conn.prepareStatement("delete from " + STABLE_NAME + " where id=?");
+ delStmt.setString(1, new String(KMIN));
+ delStmt.execute();
+ PreparedStatement upsertStmt = conn.prepareStatement("upsert into " + STABLE_NAME + " VALUES (?, ?)");
+ upsertStmt.setString(1, new String(KMIN2));
+ upsertStmt.setInt(2, 1);
+ upsertStmt.execute();
+ conn.commit();
+
+ assertFalse(waitForAsyncChange(minKeyChange,waitTime)); // Stats won't change until they're attempted to be retrieved again
+ timeKeeper.setCurrentTimeMillis(timeKeeper.currentTimeMillis() + updateFreq);
+ minKeyChange = new MinKeyChange(stats, table); // Will kick off change, but will upate asynchronously
+ assertArrayEquals(KMIN, minKeyChange.value);
+ assertTrue(waitForAsyncChange(minKeyChange,waitTime));
+ assertArrayEquals(KMIN2, stats.getMinKey(table));
+ assertArrayEquals(KMAX, stats.getMaxKey(table));
+ minKeyChange = new MinKeyChange(stats, table);
+
+ timeKeeper.setCurrentTimeMillis(timeKeeper.currentTimeMillis() + maxAge);
+ minKeyChange = new MinKeyChange(stats, table); // Will kick off change, but will upate asynchronously
+ assertTrue(null == minKeyChange.value);
+ assertTrue(waitForAsyncChange(minKeyChange,waitTime));
+ assertArrayEquals(KMIN2, stats.getMinKey(table));
+ assertArrayEquals(KMAX, stats.getMaxKey(table));
+ maxKeyChange = new MaxKeyChange(stats, table);
+
+ delStmt.setString(1, new String(KMAX));
+ delStmt.execute();
+ upsertStmt.setString(1, new String(KMAX2));
+ upsertStmt.setInt(2, 1);
+ upsertStmt.execute();
+ conn.commit();
+ conn.close();
+
+ assertFalse(waitForAsyncChange(maxKeyChange,waitTime)); // Stats won't change until they're attempted to be retrieved again
+ timeKeeper.setCurrentTimeMillis(timeKeeper.currentTimeMillis() + updateFreq);
+ maxKeyChange = new MaxKeyChange(stats, table); // Will kick off change, but will upate asynchronously
+ assertArrayEquals(KMAX, maxKeyChange.value);
+ assertTrue(waitForAsyncChange(maxKeyChange,waitTime));
+ assertArrayEquals(KMAX2, stats.getMaxKey(table));
+ assertArrayEquals(KMIN2, stats.getMinKey(table));
+ maxKeyChange = new MaxKeyChange(stats, table);
+
+ timeKeeper.setCurrentTimeMillis(timeKeeper.currentTimeMillis() + maxAge);
+ maxKeyChange = new MaxKeyChange(stats, table); // Will kick off change, but will upate asynchronously
+ assertTrue(null == maxKeyChange.value);
+ assertTrue(waitForAsyncChange(maxKeyChange,waitTime));
+ assertArrayEquals(KMIN2, stats.getMinKey(table));
+ assertArrayEquals(KMAX2, stats.getMaxKey(table));
+ }
+}