-
Notifications
You must be signed in to change notification settings - Fork 94
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
IGNITE-23434 Sql. Provide system view for active locks #4762
Merged
Merged
Changes from all commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
e492d4c
IGNITE-23434 LOCKS system view (wip).
xtern df776c4
IGNITE-23434 Test improved.
xtern 3999c3a
IGNITE-23434 Code style fix.
xtern ddf3ad0
IGNITE-23434 (minor) Added TODO to properly format lockkey.
xtern e110e41
IGNITE-23434 (minor) Strict string length.
xtern ccf05aa
IGNITE-23434 (review notes) Test refactored a bit to not use deprecat…
xtern File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
127 changes: 127 additions & 0 deletions
127
...src/integrationTest/java/org/apache/ignite/internal/sql/engine/ItLocksSystemViewTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,127 @@ | ||
/* | ||
* 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.ignite.internal.sql.engine; | ||
|
||
import static org.apache.ignite.internal.testframework.IgniteTestUtils.await; | ||
import static org.hamcrest.CoreMatchers.hasItem; | ||
import static org.hamcrest.MatcherAssert.assertThat; | ||
import static org.hamcrest.Matchers.empty; | ||
import static org.hamcrest.Matchers.emptyString; | ||
import static org.hamcrest.Matchers.equalTo; | ||
import static org.hamcrest.Matchers.is; | ||
import static org.hamcrest.Matchers.not; | ||
|
||
import java.util.Arrays; | ||
import java.util.List; | ||
import java.util.Set; | ||
import java.util.stream.Collectors; | ||
import org.apache.ignite.Ignite; | ||
import org.apache.ignite.internal.sql.BaseSqlIntegrationTest; | ||
import org.apache.ignite.internal.sql.engine.util.MetadataMatcher; | ||
import org.apache.ignite.internal.tx.InternalTransaction; | ||
import org.apache.ignite.internal.tx.LockMode; | ||
import org.apache.ignite.sql.ColumnType; | ||
import org.junit.jupiter.api.BeforeAll; | ||
import org.junit.jupiter.api.Test; | ||
|
||
/** | ||
* End-to-end tests to verify {@code LOCKS} system view. | ||
*/ | ||
public class ItLocksSystemViewTest extends BaseSqlIntegrationTest { | ||
private Set<String> nodeNames; | ||
|
||
private Set<String> lockModes; | ||
|
||
@Override | ||
protected int initialNodes() { | ||
return 2; | ||
} | ||
|
||
@BeforeAll | ||
void beforeAll() { | ||
await(systemViewManager().completeRegistration()); | ||
|
||
nodeNames = CLUSTER.runningNodes() | ||
.map(Ignite::name) | ||
.collect(Collectors.toSet()); | ||
|
||
lockModes = Arrays.stream(LockMode.values()) | ||
.map(Enum::name) | ||
.collect(Collectors.toSet()); | ||
} | ||
|
||
@Test | ||
public void testMetadata() { | ||
assertQuery("SELECT * FROM SYSTEM.LOCKS") | ||
.columnMetadata( | ||
new MetadataMatcher().name("OWNING_NODE_ID").type(ColumnType.STRING).nullable(false), | ||
new MetadataMatcher().name("TX_ID").type(ColumnType.STRING).nullable(true), | ||
new MetadataMatcher().name("OBJECT_ID").type(ColumnType.STRING).nullable(true), | ||
new MetadataMatcher().name("MODE").type(ColumnType.STRING).nullable(true) | ||
) | ||
// RO tx doesn't take locks. | ||
.returnNothing() | ||
.check(); | ||
} | ||
|
||
@Test | ||
public void testData() { | ||
Ignite node = CLUSTER.aliveNode(); | ||
|
||
sql("CREATE TABLE test (id INT PRIMARY KEY, val INT)"); | ||
sql("INSERT INTO test VALUES (0, 0), (2, 2)"); | ||
|
||
List<InternalTransaction> txs = List.of( | ||
(InternalTransaction) node.transactions().begin(), | ||
(InternalTransaction) node.transactions().begin(), | ||
(InternalTransaction) node.transactions().begin() | ||
); | ||
|
||
try { | ||
sql(txs.get(0), "INSERT INTO test VALUES (1, 1)"); | ||
sql(txs.get(1), "UPDATE test SET val = 1 WHERE id = 0"); | ||
sql(txs.get(2), "DELETE FROM test WHERE id = 2"); | ||
|
||
for (InternalTransaction tx : txs) { | ||
List<List<Object>> rows = sql("SELECT * FROM SYSTEM.LOCKS WHERE TX_ID=?", tx.id().toString()); | ||
|
||
assertThat(rows, is(not(empty()))); | ||
|
||
for (List<Object> row : rows) { | ||
verifyLockInfo(row, tx.id().toString()); | ||
} | ||
} | ||
} finally { | ||
txs.forEach(InternalTransaction::rollback); | ||
} | ||
} | ||
|
||
private void verifyLockInfo(List<Object> row, String expectedTxId) { | ||
int idx = 0; | ||
|
||
String owningNode = (String) row.get(idx++); | ||
String txId = (String) row.get(idx++); | ||
String objectId = (String) row.get(idx++); | ||
String mode = (String) row.get(idx); | ||
|
||
assertThat(nodeNames, hasItem(owningNode)); | ||
assertThat(txId, equalTo(expectedTxId)); | ||
assertThat(objectId, not(emptyString())); | ||
assertThat(lockModes, hasItem(mode)); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
55 changes: 55 additions & 0 deletions
55
...les/transactions/src/main/java/org/apache/ignite/internal/tx/views/LocksViewProvider.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,55 @@ | ||
/* | ||
* 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.ignite.internal.tx.views; | ||
|
||
import static org.apache.ignite.internal.type.NativeTypes.stringOf; | ||
|
||
import org.apache.ignite.internal.systemview.api.SystemView; | ||
import org.apache.ignite.internal.systemview.api.SystemViews; | ||
import org.apache.ignite.internal.tx.Lock; | ||
import org.apache.ignite.internal.tx.LockKey; | ||
import org.apache.ignite.internal.util.SubscriptionUtils; | ||
|
||
/** | ||
* {@code LOCKS} system view provider. | ||
*/ | ||
public class LocksViewProvider { | ||
/** Active locks. */ | ||
private final Iterable<Lock> locks; | ||
|
||
public LocksViewProvider(Iterable<Lock> locks) { | ||
this.locks = locks; | ||
} | ||
|
||
/** Returns system view exposing active locks. */ | ||
public SystemView<?> get() { | ||
return SystemViews.<Lock>nodeViewBuilder() | ||
.name("LOCKS") | ||
.nodeNameColumnAlias("OWNING_NODE_ID") | ||
.<String>addColumn("TX_ID", stringOf(36), lock -> lock.txId().toString()) | ||
.<String>addColumn("OBJECT_ID", stringOf(Short.MAX_VALUE), lock -> formatLockKey(lock.lockKey())) | ||
.<String>addColumn("MODE", stringOf(2), lock -> lock.lockMode().name()) | ||
.dataProvider(SubscriptionUtils.fromIterable(locks)) | ||
.build(); | ||
} | ||
|
||
// TODO https://issues.apache.org/jira/browse/IGNITE-23755 Provide more user-friendly information about locked objects | ||
private String formatLockKey(LockKey lockKey) { | ||
return lockKey.toString(); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
why do we need additional method if we can gather all txId and further call locks(UUID txId) for each ?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
To get active tx list we need to filter values from VolatileTxStateMetaStorage#txStateMap. This map contains not only active txs, but finished txs also (for some time). In addition, calculating the hash for each element and randomly reading elements from the map also looks less efficient than simple sequential iteration.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
got it, let`s call also @vldpyatkov here