You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@kylin.apache.org by ni...@apache.org on 2019/07/29 04:52:31 UTC
[kylin] branch master updated: KYLIN-4085 Make JDBCResourceStore
checkAndPut keep atomicity
This is an automated email from the ASF dual-hosted git repository.
nic pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/kylin.git
The following commit(s) were added to refs/heads/master by this push:
new 774b93b KYLIN-4085 Make JDBCResourceStore checkAndPut keep atomicity
774b93b is described below
commit 774b93bb28a83237123e2a7c10aad209792ee562
Author: chao long <wa...@qq.com>
AuthorDate: Tue Jul 16 10:59:46 2019 +0800
KYLIN-4085 Make JDBCResourceStore checkAndPut keep atomicity
---
.../kylin/common/persistence/JDBCResourceSQL.java | 14 ++---
.../common/persistence/JDBCResourceStore.java | 61 +++++++++-------------
.../common/persistence/JDBCSqlQueryFormat.java | 8 +--
.../main/resources/metadata-jdbc-mysql.properties | 3 +-
4 files changed, 31 insertions(+), 55 deletions(-)
diff --git a/core-common/src/main/java/org/apache/kylin/common/persistence/JDBCResourceSQL.java b/core-common/src/main/java/org/apache/kylin/common/persistence/JDBCResourceSQL.java
index 54233ea..7c7c3e2 100644
--- a/core-common/src/main/java/org/apache/kylin/common/persistence/JDBCResourceSQL.java
+++ b/core-common/src/main/java/org/apache/kylin/common/persistence/JDBCResourceSQL.java
@@ -111,17 +111,9 @@ public class JDBCResourceSQL {
return sql;
}
- public String getUpdateSqlWithoutContent() {
- final String sql = new MessageFormat(format.getUpdateSqlWithoutContent(), Locale.ROOT)
- .format(new Object[] { tableName, metaTableTs, metaTableKey, metaTableTs }, new StringBuffer(),
- new FieldPosition(0))
- .toString();
- return sql;
- }
-
- public String getUpdateContentSql() {
- final String sql = new MessageFormat(format.getUpdateContentSql(), Locale.ROOT)
- .format(new Object[] { tableName, metaTableContent, metaTableKey }, new StringBuffer(),
+ public String getUpdateContentAndTsSql() {
+ final String sql = new MessageFormat(format.getUpdateContentAndTsSql(), Locale.ROOT)
+ .format(new Object[] { tableName, metaTableTs, metaTableContent, metaTableKey, metaTableTs }, new StringBuffer(),
new FieldPosition(0))
.toString();
return sql;
diff --git a/core-common/src/main/java/org/apache/kylin/common/persistence/JDBCResourceStore.java b/core-common/src/main/java/org/apache/kylin/common/persistence/JDBCResourceStore.java
index 1c06576..61bbb98 100644
--- a/core-common/src/main/java/org/apache/kylin/common/persistence/JDBCResourceStore.java
+++ b/core-common/src/main/java/org/apache/kylin/common/persistence/JDBCResourceStore.java
@@ -456,7 +456,7 @@ public class JDBCResourceStore extends PushdownResourceStore {
int result = pstat.executeUpdate();
if (result != 1)
throw new SQLException();
- } catch (Exception e) {
+ } catch (Throwable e) {
pushdown.rollback();
throw e;
} finally {
@@ -471,43 +471,32 @@ public class JDBCResourceStore extends PushdownResourceStore {
}
} else {
// Note the checkAndPut trick:
- // update {0} set {1}=? where {2}=? and {3}=?
- pstat = connection.prepareStatement(sqls.getUpdateSqlWithoutContent());
+ // update {0} set {1}=?,{2}=? where {3}=? and {4}=?
+ pstat = connection.prepareStatement(sqls.getUpdateContentAndTsSql());
pstat.setLong(1, newTS);
- pstat.setString(2, resPath);
- pstat.setLong(3, oldTS);
- int result = pstat.executeUpdate();
- if (result != 1) {
- long realTime = getResourceTimestamp(resPath);
- throw new WriteConflictException("Overwriting conflict " + resPath + ", expect old TS "
- + oldTS + ", but it is " + realTime);
- }
- PreparedStatement pstat2 = null;
- try {
- // "update {0} set {1}=? where {3}=?"
- pstat2 = connection.prepareStatement(sqls.getUpdateContentSql());
- if (isContentOverflow(content, resPath)) {
- logger.debug("Overflow! resource path: {}, content size: {}", resPath, content.length);
- pstat2.setNull(1, Types.BLOB);
- pstat2.setString(2, resPath);
- RollbackablePushdown pushdown = writePushdown(resPath, ContentWriter.create(content));
- try {
- int result2 = pstat2.executeUpdate();
- if (result2 != 1)
- throw new SQLException();
- } catch (Exception e) {
- pushdown.rollback();
- throw e;
- } finally {
- pushdown.close();
- }
- } else {
- pstat2.setBinaryStream(1, new BufferedInputStream(new ByteArrayInputStream(content)));
- pstat2.setString(2, resPath);
- pstat2.executeUpdate();
+ pstat.setString(3, resPath);
+ pstat.setLong(4, oldTS);
+ if (isContentOverflow(content, resPath)) {
+ pstat.setNull(2, Types.BLOB);
+ RollbackablePushdown pushdown = writePushdown(resPath, ContentWriter.create(content));
+ try {
+ int result = pstat.executeUpdate();
+ if (result != 1)
+ throw new SQLException();
+ } catch (Throwable e) {
+ pushdown.rollback();
+ throw e;
+ } finally {
+ pushdown.close();
+ }
+ } else {
+ pstat.setBinaryStream(2, new BufferedInputStream(new ByteArrayInputStream(content)));
+ int result = pstat.executeUpdate();
+ if (result != 1) {
+ long realTime = getResourceTimestamp(resPath);
+ throw new WriteConflictException("Overwriting conflict " + resPath + ", expect old TS "
+ + oldTS + ", but it is " + realTime);
}
- } finally {
- JDBCConnectionManager.closeQuietly(pstat2);
}
}
}
diff --git a/core-common/src/main/java/org/apache/kylin/common/persistence/JDBCSqlQueryFormat.java b/core-common/src/main/java/org/apache/kylin/common/persistence/JDBCSqlQueryFormat.java
index dc5a9c1..392f14b 100644
--- a/core-common/src/main/java/org/apache/kylin/common/persistence/JDBCSqlQueryFormat.java
+++ b/core-common/src/main/java/org/apache/kylin/common/persistence/JDBCSqlQueryFormat.java
@@ -71,12 +71,8 @@ public class JDBCSqlQueryFormat {
return getSqlFromProperties("format.sql.insert-without-content");
}
- public String getUpdateSqlWithoutContent() {
- return getSqlFromProperties("format.sql.update-without-content");
- }
-
- public String getUpdateContentSql() {
- return getSqlFromProperties("format.sql.update-content");
+ public String getUpdateContentAndTsSql() {
+ return getSqlFromProperties("format.sql.update-content-ts");
}
public String getTestCreateSql() {
diff --git a/core-common/src/main/resources/metadata-jdbc-mysql.properties b/core-common/src/main/resources/metadata-jdbc-mysql.properties
index 3a4c386..7ce1b1a 100644
--- a/core-common/src/main/resources/metadata-jdbc-mysql.properties
+++ b/core-common/src/main/resources/metadata-jdbc-mysql.properties
@@ -26,8 +26,7 @@ format.sql.replace=update {0} set {1} = ?,{2} = ? where {3} = ?
format.sql.insert=replace into {0}({1},{2},{3}) values(?,?,?)
format.sql.replace-without-content=update {0} set {1} = ? where {2} = ?
format.sql.insert-without-content=replace into {0}({1},{2}) values(?,?)
-format.sql.update-without-content=update {0} set {1}=? where {2}=? and {3}=?
-format.sql.update-content=update {0} set {1}=? where {2}=?
+format.sql.update-content-ts=update {0} set {1}=?,{2} = ? where {3}=? and {4}=?
format.sql.test.create=create table if not exists {0} (name VARCHAR(255) primary key, id BIGINT)
format.sql.test.drop=drop table if exists {0}
format.sql.create-index=create index {0} on {1} ({2})