-
Notifications
You must be signed in to change notification settings - Fork 655
feat(keyspace): add keyspace notification support for replicas #3607
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
Open
Aetherance
wants to merge
1
commit into
apache:unstable
Choose a base branch
from
Aetherance:feat/keyspace-events-replica
base: unstable
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+270
−5
Open
Changes from all commits
Commits
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
This file contains hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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,87 @@ | ||
| /* | ||
| * 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. | ||
| */ | ||
|
|
||
| #include "cluster/replication.h" | ||
|
|
||
| #include <gtest/gtest.h> | ||
|
|
||
| #include "storage/redis_db.h" | ||
| #include "storage/redis_metadata.h" | ||
| #include "test_base.h" | ||
|
|
||
| class ReplicationWriteBatchTest : public TestBase {}; | ||
|
|
||
| TEST_F(ReplicationWriteBatchTest, ExtractKeyspaceEvents) { | ||
| rocksdb::WriteBatch batch; | ||
| auto *metadata_cf = storage_->GetCFHandle(ColumnFamilyID::Metadata); | ||
|
|
||
| redis::WriteBatchLogData set_log_data(kRedisString, {std::to_string(kRedisCmdSet)}); | ||
| ASSERT_TRUE(batch.PutLogData(set_log_data.Encode()).ok()); | ||
| std::string string_value; | ||
| Metadata(kRedisString, false).Encode(&string_value); | ||
| string_value.append("value"); | ||
| ASSERT_TRUE(batch.Put(metadata_cf, ComposeNamespaceKey("tenant", "set-key", false), string_value).ok()); | ||
|
|
||
| redis::WriteBatchLogData legacy_log_data(kRedisString); | ||
| ASSERT_TRUE(batch.PutLogData(legacy_log_data.Encode()).ok()); | ||
| ASSERT_TRUE(batch.Put(metadata_cf, ComposeNamespaceKey("tenant", "legacy-key", false), string_value).ok()); | ||
|
|
||
| redis::WriteBatchLogData del_log_data(kRedisNone, {std::to_string(kRedisCmdDel)}); | ||
| ASSERT_TRUE(batch.PutLogData(del_log_data.Encode()).ok()); | ||
| ASSERT_TRUE(batch.Delete(metadata_cf, ComposeNamespaceKey("tenant", "del-key", false)).ok()); | ||
|
|
||
| WriteBatchHandler detector(true); | ||
| ASSERT_TRUE(batch.Iterate(&detector).ok()); | ||
| ASSERT_TRUE(detector.HasKeyspaceEvents()); | ||
|
|
||
| KeyspaceEventBatchHandler handler(false); | ||
| ASSERT_TRUE(batch.Iterate(&handler).ok()); | ||
| const auto &events = handler.Events(); | ||
| ASSERT_EQ(events.size(), 2); | ||
| EXPECT_EQ(events[0].type_flag, kNotifyString); | ||
| EXPECT_EQ(events[0].event, "set"); | ||
| EXPECT_EQ(events[0].ns, "tenant"); | ||
| EXPECT_EQ(events[0].key, "set-key"); | ||
| EXPECT_EQ(events[1].type_flag, kNotifyGeneric); | ||
| EXPECT_EQ(events[1].event, "del"); | ||
| EXPECT_EQ(events[1].ns, "tenant"); | ||
| EXPECT_EQ(events[1].key, "del-key"); | ||
| } | ||
|
|
||
| TEST_F(ReplicationWriteBatchTest, IgnoreLegacyLogData) { | ||
| rocksdb::WriteBatch batch; | ||
| redis::WriteBatchLogData legacy_log_data(kRedisString); | ||
| ASSERT_TRUE(batch.PutLogData(legacy_log_data.Encode()).ok()); | ||
|
|
||
| std::string string_value; | ||
| Metadata(kRedisString, false).Encode(&string_value); | ||
| string_value.append("value"); | ||
| ASSERT_TRUE(batch | ||
| .Put(storage_->GetCFHandle(ColumnFamilyID::Metadata), | ||
| ComposeNamespaceKey(kDefaultNamespace, "key", false), string_value) | ||
| .ok()); | ||
|
|
||
| WriteBatchHandler detector(true); | ||
| ASSERT_TRUE(batch.Iterate(&detector).ok()); | ||
| EXPECT_FALSE(detector.HasKeyspaceEvents()); | ||
|
|
||
| KeyspaceEventBatchHandler handler(false); | ||
| ASSERT_TRUE(batch.Iterate(&handler).ok()); | ||
| EXPECT_TRUE(handler.Events().empty()); | ||
| } |
This file contains hidden or 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
Oops, something went wrong.
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.
Hi @Aetherance.
We already declare the keyspace event in the existing code. Why do we need to maintain another separate set of event-related logic here?
Could we make each operation declare the event only once, and let a unified batch / commit path generate the
LogDatarequired for replication and handle the local notification after the write is committed successfully? This would avoid maintaining the semantics of the same event separately in the local notification and replication paths.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.
This is because we need to reuse the existing
PutLogDatalogic inupdateRawValue. SinceupdateRawValueis shared by multiple write operations, the lower layer cannot determine which command was executed unless the specific event type is passed down.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.
I think it is reasonable to keep this logic here because this LogData may also be inspected by code paths other than the keyspace notification mechanism. Therefore, it should not be merged into keyspace-event-specific logic.
Uh oh!
There was an error while loading. Please reload this page.
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.
Additionally, this API:
is used only by string operations, and all string operation requires only a single change, so this will not result in widespread API changes. For commands such as DEL, only one line of code needs to be changed.