Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fixes #1654: Switch to a spin pg_try_advisory_lock #1656

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
Expand Up @@ -20,8 +20,11 @@
import org.flywaydb.core.internal.dbsupport.JdbcTemplate;
import org.flywaydb.core.api.logging.Log;
import org.flywaydb.core.api.logging.LogFactory;
import org.flywaydb.core.internal.util.jdbc.RowMapper;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.List;
import java.util.concurrent.Callable;

/**
Expand Down Expand Up @@ -64,7 +67,7 @@ public class PostgreSQLAdvisoryLockTemplate {
*/
public <T> T execute(Callable<T> callable) {
try {
jdbcTemplate.execute("SELECT pg_advisory_lock(" + lockNum + ")");
lock();
return callable.call();
} catch (SQLException e) {
throw new FlywaySqlException("Unable to acquire Flyway advisory lock", e);
Expand All @@ -84,4 +87,26 @@ public <T> T execute(Callable<T> callable) {
}
}
}
}

private void lock() throws SQLException {
while (!tryLock()) {
try {
Thread.sleep(100L);
} catch (InterruptedException e) {
throw new FlywayException("Interrupted while attempting to acquire Flyway advisory lock", e);
}
}
}

private boolean tryLock() throws SQLException {
List<Boolean> results = jdbcTemplate.query(
"SELECT pg_try_advisory_lock(" + lockNum + ")",
new RowMapper<Boolean>() {
@Override
public Boolean mapRow(ResultSet rs) throws SQLException {
return "t".equals(rs.getString("pg_try_advisory_lock"));
}
});
return results.size() == 1 && results.get(0);
}
}
Expand Up @@ -56,4 +56,16 @@ protected DataSource createDataSource(Properties customProperties) {
return new DriverDataSource(Thread.currentThread().getContextClassLoader(), null,
jdbcUrl, JDBC_USER, JDBC_PASSWORD);
}

protected String getBasedir() {
return "migration/dbsupport/postgresql/sql/concurrent";
}

@Override
protected boolean isMixed() {
// V1_1__View.sql has both a SELECT pg_sleep and CREATE INDEX CONCURRENTLY to help
// to reproduce the deadlock which can occur with the use of advisory locks.
// See #1654 for details.
return true;
}
}
Expand Up @@ -113,6 +113,10 @@ protected String getSchemaName(DbSupport dbSupport) {
return "concurrent_test";
}

protected boolean isMixed() {
return false;
}

/**
* Creates the datasource for this testcase based on these optional custom properties from the user home.
*
Expand Down Expand Up @@ -178,6 +182,7 @@ private Flyway createFlyway() throws SQLException {
placeholders.put("schema", schemaQuoted);

newFlyway.setPlaceholders(placeholders);
newFlyway.setMixed(isMixed());
newFlyway.setBaselineVersionAsString("0.1");
return newFlyway;
}
Expand Down
@@ -0,0 +1,25 @@
--
-- Copyright 2010-2017 Boxfuse GmbH
--
-- Licensed 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.
--

CREATE VIEW ${schema}.all_misters AS SELECT * FROM ${schema}.test_user WHERE name LIKE 'Mr.%';

-- The pg_sleep is just to simulate creating an index concurrently
-- on a large table which can trigger a deadlock in combination with
-- the use of advisory locks. The sleep is not strictly required for
-- the deadlock to occur.
SELECT pg_sleep(1);

CREATE INDEX CONCURRENTLY idx_test_user_name ON ${schema}.test_user(name);
@@ -0,0 +1,18 @@
--
-- Copyright 2010-2017 Boxfuse GmbH
--
-- Licensed 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.
--

INSERT INTO ${schema}.test_user (name, id) VALUES ('Mr. Iße T', 1);
INSERT INTO ${schema}.test_user (name, id) VALUES ('Mr. Semicolon;', 2);
@@ -0,0 +1,21 @@
--
-- Copyright 2010-2017 Boxfuse GmbH
--
-- Licensed 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.
--

CREATE TABLE ${schema}.test_user (
id INT NOT NULL,
name VARCHAR(25) NOT NULL, -- this is a valid comment
PRIMARY KEY(name) /* and so is this ! */
);
@@ -0,0 +1,26 @@
--
-- Copyright 2010-2017 Boxfuse GmbH
--
-- Licensed 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.
--

CREATE TABLE ${schema}.couple (
id INT NOT NULL,
name1 VARCHAR(25) NOT NULL,
name2 VARCHAR(25) NOT NULL,
PRIMARY KEY(id),
CONSTRAINT couple_user1_fk FOREIGN KEY (name1) REFERENCES ${schema}.test_user(name),
CONSTRAINT couple_user2_fk FOREIGN KEY (name2) REFERENCES ${schema}.test_user(name)
);

INSERT INTO ${schema}.couple (id, name1, name2) VALUES (1, 'Mr. Iße T', 'Mr. Semicolon;');