Skip to content

HBASE-26850 Optimize the implementation of LRUCache in LRUDictionary - #4233

Closed
thangTang wants to merge 1 commit into
apache:branch-1from
thangTang:HBASE-26850
Closed

HBASE-26850 Optimize the implementation of LRUCache in LRUDictionary#4233
thangTang wants to merge 1 commit into
apache:branch-1from
thangTang:HBASE-26850

Conversation

@thangTang

Copy link
Copy Markdown
Contributor

No description provided.

@Apache-HBase

Copy link
Copy Markdown

🎊 +1 overall

VoteSubsystemRuntimeComment
+0 🆗reexec1m 14sDocker mode activated.
_ Prechecks _
+1 💚dupname0m 0sNo case conflicting files found.
+1 💚hbaseanti0m 0sPatch does not have any anti-patterns.
+1 💚@author0m 0sThe patch does not contain any @author tags.
+1 💚test4tests0m 0sThe patch appears to include 1 new or modified test files.
_ branch-1 Compile Tests _
+1 💚mvninstall4m 17sbranch-1 passed
+1 💚compile0m 21sbranch-1 passed with JDK Azul Systems, Inc.-1.8.0_262-b19
+1 💚compile0m 22sbranch-1 passed with JDK Azul Systems, Inc.-1.7.0_272-b10
+1 💚checkstyle0m 27sbranch-1 passed
+1 💚shadedjars2m 52sbranch has no errors when building our shaded downstream artifacts.
+1 💚javadoc0m 23sbranch-1 passed with JDK Azul Systems, Inc.-1.8.0_262-b19
+1 💚javadoc0m 22sbranch-1 passed with JDK Azul Systems, Inc.-1.7.0_272-b10
+0 🆗spotbugs1m 7sUsed deprecated FindBugs config; considering switching to SpotBugs.
+1 💚findbugs1m 4sbranch-1 passed
_ Patch Compile Tests _
+1 💚mvninstall2m 8sthe patch passed
+1 💚compile0m 21sthe patch passed with JDK Azul Systems, Inc.-1.8.0_262-b19
+1 💚javac0m 21sthe patch passed
+1 💚compile0m 24sthe patch passed with JDK Azul Systems, Inc.-1.7.0_272-b10
+1 💚javac0m 24sthe patch passed
+1 💚checkstyle0m 27sthe patch passed
+1 💚whitespace0m 0sThe patch has no whitespace issues.
+1 💚shadedjars2m 48spatch has no errors when building our shaded downstream artifacts.
+1 💚hadoopcheck4m 53sPatch does not cause any errors with Hadoop 2.8.5 2.9.2.
+1 💚javadoc0m 21sthe patch passed with JDK Azul Systems, Inc.-1.8.0_262-b19
+1 💚javadoc0m 22sthe patch passed with JDK Azul Systems, Inc.-1.7.0_272-b10
+1 💚findbugs1m 14sthe patch passed
_ Other Tests _
+1 💚unit2m 30shbase-common in the patch passed.
+1 💚asflicense0m 14sThe patch does not generate ASF License warnings.
31m 31s
SubsystemReport/Notes
DockerClientAPI=1.41 ServerAPI=1.41 base: https://ci-hbase.apache.org/job/HBase-PreCommit-GitHub-PR/job/PR-4233/1/artifact/out/Dockerfile
GITHUB PR#4233
Optional Testsdupname asflicense javac javadoc unit spotbugs findbugs shadedjars hadoopcheck hbaseanti checkstyle compile
unameLinux f89a2a8fc107 5.4.0-90-generic #101-Ubuntu SMP Fri Oct 15 20:00:55 UTC 2021 x86_64 x86_64 x86_64 GNU/Linux
Build toolmaven
Personality/home/jenkins/jenkins-home/workspace/Base-PreCommit-GitHub-PR_PR-4233/out/precommit/personality/provided.sh
git revisionbranch-1 / 70e695b
Default JavaAzul Systems, Inc.-1.7.0_272-b10
Multi-JDK versions/usr/lib/jvm/zulu-8-amd64:Azul Systems, Inc.-1.8.0_262-b19 /usr/lib/jvm/zulu-7-amd64:Azul Systems, Inc.-1.7.0_272-b10
Test Resultshttps://ci-hbase.apache.org/job/HBase-PreCommit-GitHub-PR/job/PR-4233/1/testReport/
Max. process+thread count143 (vs. ulimit of 10000)
modulesC: hbase-common U: hbase-common
Console outputhttps://ci-hbase.apache.org/job/HBase-PreCommit-GitHub-PR/job/PR-4233/1/console
versionsgit=2.17.1 maven=3.6.0 findbugs=3.0.1
Powered byApache Yetus 0.12.0 https://yetus.apache.org

This message was automatically generated.

@Apache-HBase

Copy link
Copy Markdown

🎊 +1 overall

VoteSubsystemRuntimeComment
+0 🆗reexec1m 4sDocker mode activated.
_ Prechecks _
+1 💚dupname0m 0sNo case conflicting files found.
+1 💚hbaseanti0m 0sPatch does not have any anti-patterns.
+1 💚@author0m 0sThe patch does not contain any @author tags.
+1 💚test4tests0m 0sThe patch appears to include 1 new or modified test files.
_ branch-1 Compile Tests _
+1 💚mvninstall2m 53sbranch-1 passed
+1 💚compile0m 13sbranch-1 passed with JDK Azul Systems, Inc.-1.8.0_262-b19
+1 💚compile0m 17sbranch-1 passed with JDK Azul Systems, Inc.-1.7.0_272-b10
+1 💚checkstyle0m 19sbranch-1 passed
+1 💚shadedjars1m 45sbranch has no errors when building our shaded downstream artifacts.
+1 💚javadoc0m 16sbranch-1 passed with JDK Azul Systems, Inc.-1.8.0_262-b19
+1 💚javadoc0m 16sbranch-1 passed with JDK Azul Systems, Inc.-1.7.0_272-b10
+0 🆗spotbugs0m 44sUsed deprecated FindBugs config; considering switching to SpotBugs.
+1 💚findbugs0m 42sbranch-1 passed
_ Patch Compile Tests _
+1 💚mvninstall1m 14sthe patch passed
+1 💚compile0m 13sthe patch passed with JDK Azul Systems, Inc.-1.8.0_262-b19
+1 💚javac0m 13sthe patch passed
+1 💚compile0m 16sthe patch passed with JDK Azul Systems, Inc.-1.7.0_272-b10
+1 💚javac0m 16sthe patch passed
+1 💚checkstyle0m 18sthe patch passed
+1 💚whitespace0m 0sThe patch has no whitespace issues.
+1 💚shadedjars1m 43spatch has no errors when building our shaded downstream artifacts.
+1 💚hadoopcheck2m 55sPatch does not cause any errors with Hadoop 2.8.5 2.9.2.
+1 💚javadoc0m 13sthe patch passed with JDK Azul Systems, Inc.-1.8.0_262-b19
+1 💚javadoc0m 16sthe patch passed with JDK Azul Systems, Inc.-1.7.0_272-b10
+1 💚findbugs0m 47sthe patch passed
_ Other Tests _
+1 💚unit1m 53shbase-common in the patch passed.
+1 💚asflicense0m 12sThe patch does not generate ASF License warnings.
20m 11s
SubsystemReport/Notes
DockerClientAPI=1.41 ServerAPI=1.41 base: https://ci-hbase.apache.org/job/HBase-PreCommit-GitHub-PR/job/PR-4233/2/artifact/out/Dockerfile
GITHUB PR#4233
Optional Testsdupname asflicense javac javadoc unit spotbugs findbugs shadedjars hadoopcheck hbaseanti checkstyle compile
unameLinux 7c6178f9c255 5.4.0-90-generic #101-Ubuntu SMP Fri Oct 15 20:00:55 UTC 2021 x86_64 x86_64 x86_64 GNU/Linux
Build toolmaven
Personality/home/jenkins/jenkins-home/workspace/Base-PreCommit-GitHub-PR_PR-4233/out/precommit/personality/provided.sh
git revisionbranch-1 / aa9cba3
Default JavaAzul Systems, Inc.-1.7.0_272-b10
Multi-JDK versions/usr/lib/jvm/zulu-8-amd64:Azul Systems, Inc.-1.8.0_262-b19 /usr/lib/jvm/zulu-7-amd64:Azul Systems, Inc.-1.7.0_272-b10
Test Resultshttps://ci-hbase.apache.org/job/HBase-PreCommit-GitHub-PR/job/PR-4233/2/testReport/
Max. process+thread count161 (vs. ulimit of 10000)
modulesC: hbase-common U: hbase-common
Console outputhttps://ci-hbase.apache.org/job/HBase-PreCommit-GitHub-PR/job/PR-4233/2/console
versionsgit=2.17.1 maven=3.6.0 findbugs=3.0.1
Powered byApache Yetus 0.12.0 https://yetus.apache.org

This message was automatically generated.

if (nodeToIndex.containsKey(node)) {
short index = nodeToIndex.get(node);
node = indexToNode[index];
moveToHead(node);

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

According to discuss in the email, if here always use the previous node, then how can it ensure that the previous node is a completed one?

@thangTangthangTangMar 18, 2022

Copy link
Copy Markdown
ContributorAuthor

Choose a reason for hiding this comment

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

Sorry, I didn't understand what "completed one" means.
But this patch does not actually change the logic of the previous use of this LRUCache:
RingBufferEventHandler#onEvent -> RingBufferEventHandler#append -> ProtobufLogWriter#append -> CompressedKvEncoder#write -> LRUDictionary#findEntry
For this actually used write link, findEntry uses the previously existing node.
We did find NPE on the read link, but the root reason is not the implementation of this LRUCache (this patch), but that the LRUCache is polluted.
I just unified the logic of addEntry with the actual logic on the write link, which I think is more elegant.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

The call trace is WALEntryStream#tryAdvanceEntry->ProtobufLogReader#readNext->CompressedKVDecoder#readIntoArray->LRUDirectory#addEntry->then here the changed BidirectionalLRUMap#put.
Your change only makes the newly some node will not be added to the directory, but the old same node may have uncompleted data, e.g. tailing the WAL.

Copy link
Copy Markdown
ContributorAuthor

Choose a reason for hiding this comment

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

Oh i got your point. As we discussed in email, to solve the problem you mentioned, we need to rebuild the LRUCache every time when we re-seek to somewhere(will done it in 26849), and in the future we could try to implement a 'versioned' cache for replication.
This patch is just for code optimization, not to solve the problem. So it's an "Improvement", not a "bug".
Could this answer your question?

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Since the new value maybe a completed one, this improvement can not prove using the old value is always better than the new value, except the performance improvement.
I think an umbrella should be created to track the problem mentioned in the email, and this issue can be a child of it. So before the umbrella issue is completed, all the child codes can be tested together.
Thanks.

Copy link
Copy Markdown
ContributorAuthor

Choose a reason for hiding this comment

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

From a stability or performance standpoint, I don't think it's a good or bad/right or wrong question since it doesn't change the existing logic.
But from a code architecture point of view, I think this way is better. The original implementation is to put the logic of "find the existing node and return" into findEntry, and directly expose addEntry to the outside, which leads to the possibility of inconsistent behavior between the two. So I think we can completely encapsulate the same logic in addEntry (although this does not bring any stability improvement for now).
But if you would like to wait for 26849 to finish and watch it together, I think it's OK~

@apurtellapurtellApr 17, 2022

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

I am not sure I follow the discussion because in the proposed improvement the old node is not reused if the contents being stored are different.

 Node node = new Node();
node.setContents(stored, 0, stored.length);
if (nodeToIndex.containsKey(node)) {
// new logic reusing existing entry and index
// ...
} else {
// original logic adding new entry
// ...
} 

containsKey will use hashcode of Node, which is Bytes.hashCode over the contents. A previous short read and a current full read will have different contents so different hashcode, right? If so, this just reuses an entry that has equivalent data, which I agree is an improvement.

Copy link
Copy Markdown
ContributorAuthor

Choose a reason for hiding this comment

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

the old node is not reused if the contents being stored are different.

Completely correct.
This patch only reuse SAME node.
Actually, In the previous implementation, if the nodes are same, the existing nodes will also be reused too, the only difference is this logic were in findIdx:

private short findIdx(byte[] array, int offset, int length) {
Short s;
final Node comparisonNode = new Node();
comparisonNode.setContents(array, offset, length);
if ((s = nodeToIndex.get(comparisonNode)) != null) {
moveToHead(indexToNode[s]);
return s;
} else {
return -1;
}
}

For the write link:

CompressedKvEncoder#write
->
LRUDictionary#findEntry (LRUDictionary#findIdx)
->
LRUDictionary#addEntry

But for the read link:

CompressedKVDecoder#readIntoArray
->
LRUDirectory#addEntry

We could see, on the read link, it just addEntry directly, without findIdx(reuse the existing same node).
So, I just thought it would be more beautiful to write this way.

Sign up for freeto join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

5 participants

@thangTang@Apache-HBase@apurtell@sunhelly@Apache9