Repository: phoenix Updated Branches: refs/heads/master ebb6a7adb -> 355369ad5
PHOENIX-1175 Add setReadOnly and setFetchSize options (Alex Kamil) Project: http://git-wip-us.apache.org/repos/asf/phoenix/repo Commit: http://git-wip-us.apache.org/repos/asf/phoenix/commit/355369ad Tree: http://git-wip-us.apache.org/repos/asf/phoenix/tree/355369ad Diff: http://git-wip-us.apache.org/repos/asf/phoenix/diff/355369ad Branch: refs/heads/master Commit: 355369ad5bd9a61d1b19e6bebec32ea8cb8b8a0c Parents: ebb6a7a Author: James Taylor <jtay...@salesforce.com> Authored: Fri Aug 15 23:56:34 2014 -0700 Committer: James Taylor <jtay...@salesforce.com> Committed: Fri Aug 15 23:56:34 2014 -0700 ---------------------------------------------------------------------- .../org/apache/phoenix/end2end/ReadOnlyIT.java | 98 ++++++++++++++++++++ .../phoenix/exception/SQLExceptionCode.java | 7 +- .../apache/phoenix/jdbc/PhoenixConnection.java | 9 +- .../apache/phoenix/jdbc/PhoenixStatement.java | 16 +++- 4 files changed, 120 insertions(+), 10 deletions(-) ---------------------------------------------------------------------- http://git-wip-us.apache.org/repos/asf/phoenix/blob/355369ad/phoenix-core/src/it/java/org/apache/phoenix/end2end/ReadOnlyIT.java ---------------------------------------------------------------------- diff --git a/phoenix-core/src/it/java/org/apache/phoenix/end2end/ReadOnlyIT.java b/phoenix-core/src/it/java/org/apache/phoenix/end2end/ReadOnlyIT.java new file mode 100644 index 0000000..515acac --- /dev/null +++ b/phoenix-core/src/it/java/org/apache/phoenix/end2end/ReadOnlyIT.java @@ -0,0 +1,98 @@ +/* + * 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.TEST_PROPERTIES; +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertFalse; +import static org.junit.Assert.assertTrue; +import static org.junit.Assert.fail; + +import java.sql.Connection; +import java.sql.DriverManager; +import java.sql.PreparedStatement; +import java.sql.ResultSet; +import java.sql.SQLException; +import java.util.Properties; +import java.sql.ResultSetMetaData; + +import org.apache.phoenix.util.PropertiesUtil; +import org.junit.Test; +import org.junit.experimental.categories.Category; + +@Category(HBaseManagedTimeTest.class) +public class ReadOnlyIT extends BaseHBaseManagedTimeIT { + + @Test + public void testConnectionReadOnly() throws Exception { + + Properties props = PropertiesUtil.deepCopy(TEST_PROPERTIES); + Connection conn = DriverManager.getConnection(getUrl(), props); + String ddl = "CREATE TABLE test_table " + + " (row varchar not null, col1 integer" + + " CONSTRAINT pk PRIMARY KEY (row))\n"; + createTestTable(getUrl(), ddl); + + String query = "UPSERT INTO test_table(row, col1) VALUES('row1', 777)"; + PreparedStatement statement = conn.prepareStatement(query); + statement.executeUpdate(); + conn.commit(); + + try{ + conn.setReadOnly(true); + assertTrue(conn.isReadOnly()); + ddl = "CREATE TABLE test_table2 " + + " (row varchar not null, col1 integer" + + " CONSTRAINT pk PRIMARY KEY (row))\n"; + statement = conn.prepareStatement(ddl); + statement.executeUpdate(); + conn.commit(); + fail(); + } catch (SQLException e) { + assertTrue(e.getMessage(), e.getMessage().contains("ERROR 518 (25502): Mutations are not permitted for a read-only connection.")); + } + + try { + query = "UPSERT INTO test_table(row, col1) VALUES('row1', 888)"; + statement = conn.prepareStatement(query); + statement.executeUpdate(); + conn.commit(); + fail(); + } catch (SQLException e) { + assertTrue(e.getMessage(), e.getMessage().contains("ERROR 518 (25502): Mutations are not permitted for a read-only connection.")); + } + + conn.setReadOnly(false); + assertFalse(conn.isReadOnly()); + ddl = "ALTER TABLE test_table ADD col2 VARCHAR"; + statement = conn.prepareStatement(ddl); + statement.executeUpdate(); + conn.commit(); + + try { + conn.setReadOnly(true); + ddl = "ALTER TABLE test_table ADD col3 VARCHAR"; + statement = conn.prepareStatement(ddl); + statement.executeUpdate(); + fail(); + } catch (SQLException e) { + assertTrue(e.getMessage(), e.getMessage().contains("ERROR 518 (25502): Mutations are not permitted for a read-only connection.")); + } + + } +} http://git-wip-us.apache.org/repos/asf/phoenix/blob/355369ad/phoenix-core/src/main/java/org/apache/phoenix/exception/SQLExceptionCode.java ---------------------------------------------------------------------- diff --git a/phoenix-core/src/main/java/org/apache/phoenix/exception/SQLExceptionCode.java b/phoenix-core/src/main/java/org/apache/phoenix/exception/SQLExceptionCode.java index f8cbd87..3d5e302 100644 --- a/phoenix-core/src/main/java/org/apache/phoenix/exception/SQLExceptionCode.java +++ b/phoenix-core/src/main/java/org/apache/phoenix/exception/SQLExceptionCode.java @@ -140,7 +140,12 @@ public enum SQLExceptionCode { ORDER_BY_ARRAY_NOT_SUPPORTED(515, "42893", "ORDER BY of an array type is not allowed"), NON_EQUALITY_ARRAY_COMPARISON(516, "42894", "Array types may only be compared using = or !="), INVALID_NOT_NULL_CONSTRAINT(517, "42895", "Invalid not null constraint on non primary key column"), - + + /** + * Invalid Transaction State (errorcode 05, sqlstate 25) + */ + READ_ONLY_CONNECTION(518,"25502","Mutations are not permitted for a read-only connection."), + /** * HBase and Phoenix specific implementation defined sub-classes. * Column family related exceptions. http://git-wip-us.apache.org/repos/asf/phoenix/blob/355369ad/phoenix-core/src/main/java/org/apache/phoenix/jdbc/PhoenixConnection.java ---------------------------------------------------------------------- diff --git a/phoenix-core/src/main/java/org/apache/phoenix/jdbc/PhoenixConnection.java b/phoenix-core/src/main/java/org/apache/phoenix/jdbc/PhoenixConnection.java index 70f88f2..650fedf 100644 --- a/phoenix-core/src/main/java/org/apache/phoenix/jdbc/PhoenixConnection.java +++ b/phoenix-core/src/main/java/org/apache/phoenix/jdbc/PhoenixConnection.java @@ -125,7 +125,8 @@ public class PhoenixConnection implements Connection, org.apache.phoenix.jdbc.Jd private boolean isClosed = false; private Sampler<?> sampler; - + private boolean readOnly = false; + static { // add the phoenix span receiver so we can log the traces. We have a single trace // source for the whole JVM @@ -519,7 +520,7 @@ public class PhoenixConnection implements Connection, org.apache.phoenix.jdbc.Jd @Override public boolean isReadOnly() throws SQLException { - return true; + return readOnly; } @Override @@ -631,9 +632,7 @@ public class PhoenixConnection implements Connection, org.apache.phoenix.jdbc.Jd @Override public void setReadOnly(boolean readOnly) throws SQLException { - if (readOnly) { - throw new SQLFeatureNotSupportedException(); - } + this.readOnly=readOnly; } @Override http://git-wip-us.apache.org/repos/asf/phoenix/blob/355369ad/phoenix-core/src/main/java/org/apache/phoenix/jdbc/PhoenixStatement.java ---------------------------------------------------------------------- diff --git a/phoenix-core/src/main/java/org/apache/phoenix/jdbc/PhoenixStatement.java b/phoenix-core/src/main/java/org/apache/phoenix/jdbc/PhoenixStatement.java index 42acc60..870adc4 100644 --- a/phoenix-core/src/main/java/org/apache/phoenix/jdbc/PhoenixStatement.java +++ b/phoenix-core/src/main/java/org/apache/phoenix/jdbc/PhoenixStatement.java @@ -173,7 +173,7 @@ public class PhoenixStatement implements Statement, SQLCloseable, org.apache.pho private Operation lastUpdateOperation; private boolean isClosed = false; private int maxRows; - + private int fetchSize = -1; public PhoenixStatement(PhoenixConnection connection) { this.connection = connection; @@ -237,6 +237,11 @@ public class PhoenixStatement implements Statement, SQLCloseable, org.apache.pho } protected int executeMutation(final CompilableStatement stmt) throws SQLException { + if (connection.isReadOnly()) { + throw new SQLExceptionInfo.Builder( + SQLExceptionCode.READ_ONLY_CONNECTION). + build().buildException(); + } try { return CallRunner .run( @@ -1012,7 +1017,10 @@ public class PhoenixStatement implements Statement, SQLCloseable, org.apache.pho @Override public int getFetchSize() throws SQLException { - return connection.getQueryServices().getProps().getInt(QueryServices.SCAN_CACHE_SIZE_ATTRIB, QueryServicesOptions.DEFAULT_SCAN_CACHE_SIZE); + if (fetchSize>0) + return fetchSize; + else + return connection.getQueryServices().getProps().getInt(QueryServices.SCAN_CACHE_SIZE_ATTRIB, QueryServicesOptions.DEFAULT_SCAN_CACHE_SIZE); } @Override @@ -1120,9 +1128,9 @@ public class PhoenixStatement implements Statement, SQLCloseable, org.apache.pho } @Override - public void setFetchSize(int rows) throws SQLException { + public void setFetchSize(int fetchSize) throws SQLException { // TODO: map to Scan.setBatch() ? - throw new SQLFeatureNotSupportedException(); + this.fetchSize = fetchSize; } @Override