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

speeds up fate lock acquisition #5262

Merged
merged 12 commits into from
Feb 1, 2025
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@
public class FateLock implements QueueLock {
private static final Logger log = LoggerFactory.getLogger(FateLock.class);

private static final String PREFIX = "flock#";
static final String PREFIX = "flock#";

private final ZooReaderWriter zoo;
private final FateLockPath path;
Expand Down Expand Up @@ -78,11 +78,11 @@ public static class FateLockNode implements Comparable<FateLockNode> {
public final long sequence;
public final String lockData;

private FateLockNode(String nodeName) {
FateLockNode(String nodeName) {
int len = nodeName.length();
Preconditions.checkArgument(nodeName.startsWith(PREFIX) && nodeName.charAt(len - 11) == '#',
"Illegal node name %s", nodeName);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

In the case of an overflow, the character at (len - 11) be a minus sign and cause this to fail.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It depends on the number of digits in the negative number. If its a 10 digit negative number then %010d will produce a 11 char string. If its 9 digits or less then %010d will produce a 10 char string including the negative. For the 9 digit or less case the # check will not catch negative, but the later Long.parseUnsignedLong call will catch it.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

For the case Long.parseUnsignedLong catches the problem it throws a more specific exception, I updated the test to check for that.

sequence = Long.parseLong(nodeName.substring(len - 10));
sequence = Long.parseUnsignedLong(nodeName.substring(len - 10), 10);
lockData = nodeName.substring(PREFIX.length(), len - 11);
}

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
/*
* 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
*
* https://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.accumulo.core.fate.zookeeper;

import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertThrows;

import java.util.UUID;

import org.apache.accumulo.core.fate.FateId;
import org.apache.accumulo.core.fate.FateInstanceType;
import org.junit.jupiter.api.Test;

public class FateLockTest {

@Test
public void testParsing() {
var fateId = FateId.from(FateInstanceType.USER, UUID.randomUUID());
// ZooKeeper docs state that sequence numbers are formatted using %010d
var lockNode = new FateLock.FateLockNode(
FateLock.PREFIX + fateId.canonical() + "#" + String.format("%010d", 40));
assertEquals(40, lockNode.sequence);
assertEquals(fateId.canonical(), lockNode.lockData);

assertThrows(IllegalArgumentException.class,
() -> new FateLock.FateLockNode(fateId.canonical() + "#" + String.format("%010d", 40)));
assertThrows(IllegalArgumentException.class, () -> new FateLock.FateLockNode(
FateLock.PREFIX + fateId.canonical() + "#" + String.format("%d", 40)));
assertThrows(IllegalArgumentException.class, () -> new FateLock.FateLockNode(
FateLock.PREFIX + fateId.canonical() + "#" + String.format("%09d", 40)));
assertThrows(IllegalArgumentException.class, () -> new FateLock.FateLockNode(
FateLock.PREFIX + fateId.canonical() + "#" + String.format("%011d", 40)));
assertThrows(IllegalArgumentException.class,
() -> new FateLock.FateLockNode(FateLock.PREFIX + fateId.canonical() + "#abc"));
assertThrows(IllegalArgumentException.class, () -> new FateLock.FateLockNode(
FateLock.PREFIX + fateId.canonical() + String.format("%010d", 40)));

// ZooKeeper docs state that sequence numbers can roll and become negative. The FateLock code
// does not support this, so make sure it fails if this happens.
for (int i : new int[] {Integer.MIN_VALUE, Integer.MIN_VALUE / 2, Integer.MIN_VALUE / 10,
Integer.MIN_VALUE / 1000, -40}) {
assertThrows(IllegalArgumentException.class, () -> new FateLock.FateLockNode(
FateLock.PREFIX + fateId.canonical() + "#" + String.format("%010d", i)));
}

assertThrows(IllegalArgumentException.class, () -> new FateLock.FateLockNode(
FateLock.PREFIX + fateId.canonical() + "#" + String.format("%d", -40)));
}
}
Loading