-
Notifications
You must be signed in to change notification settings - Fork 7
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
Verify keys on read #495
Verify keys on read #495
Changes from 9 commits
9b95b49
4003615
bf7679b
65ef7d4
6acc3d3
82584c2
d8f67b8
8e53465
1275f52
052306e
745468b
e01cd05
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,60 @@ | ||
/* | ||
* 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 com.palantir.cassandra.utils; | ||
|
||
import com.palantir.logsafe.SafeArg; | ||
import com.palantir.logsafe.UnsafeArg; | ||
import org.apache.cassandra.db.Keyspace; | ||
import org.apache.cassandra.exceptions.InvalidMutationException; | ||
import org.apache.cassandra.utils.FBUtilities; | ||
import org.apache.cassandra.utils.Hex; | ||
import org.slf4j.Logger; | ||
import org.slf4j.LoggerFactory; | ||
|
||
import java.net.InetAddress; | ||
import java.nio.ByteBuffer; | ||
import java.util.Collection; | ||
import java.util.List; | ||
|
||
public class MutationVerificationHandler implements OwnershipVerificationHandler | ||
{ | ||
private static final Logger logger = LoggerFactory.getLogger(OwnershipVerificationUtils.class); | ||
|
||
public static final OwnershipVerificationHandler INSTANCE = new MutationVerificationHandler(); | ||
|
||
@Override | ||
public void onViolation(Keyspace keyspace, ByteBuffer key, List<InetAddress> naturalEndpoints, Collection<InetAddress> pendingEndpoints) | ||
{ | ||
keyspace.metric.invalidMutations.inc(); | ||
logger.error( | ||
"InvalidMutation! Cannot apply mutation as this host {} does not contain key {} in keyspace {}. Only hosts {} and {} do.", | ||
SafeArg.of("address", FBUtilities.getBroadcastAddress()), | ||
UnsafeArg.of("key", Hex.bytesToHex(key.array())), | ||
SafeArg.of("keyspace", keyspace.getName()), | ||
SafeArg.of("naturalEndpoints", naturalEndpoints), | ||
SafeArg.of("pendingEndpoints", pendingEndpoints)); | ||
throw new InvalidMutationException(); | ||
} | ||
|
||
@Override | ||
public void onValid(Keyspace keyspace) | ||
{ | ||
keyspace.metric.validMutations.inc(); | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
/* | ||
* 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 com.palantir.cassandra.utils; | ||
|
||
import org.apache.cassandra.db.Keyspace; | ||
|
||
import java.net.InetAddress; | ||
import java.nio.ByteBuffer; | ||
import java.util.Collection; | ||
import java.util.List; | ||
|
||
public interface OwnershipVerificationHandler | ||
{ | ||
void onViolation(Keyspace keyspace, ByteBuffer key, List<InetAddress> naturalEndpoints, Collection<InetAddress> pendingEndpoints); | ||
|
||
void onValid(Keyspace keyspace); | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -19,16 +19,18 @@ | |
package com.palantir.cassandra.utils; | ||
|
||
import java.net.InetAddress; | ||
import java.nio.ByteBuffer; | ||
import java.time.Duration; | ||
import java.time.Instant; | ||
import java.util.Collection; | ||
import java.util.List; | ||
|
||
import com.google.common.annotations.VisibleForTesting; | ||
import org.apache.cassandra.exceptions.InvalidMutationException; | ||
import org.slf4j.Logger; | ||
import org.slf4j.LoggerFactory; | ||
|
||
import com.palantir.logsafe.SafeArg; | ||
import com.palantir.logsafe.UnsafeArg; | ||
import org.apache.cassandra.db.Keyspace; | ||
import org.apache.cassandra.db.Mutation; | ||
import org.apache.cassandra.dht.Token; | ||
|
@@ -37,72 +39,75 @@ | |
import org.apache.cassandra.utils.FBUtilities; | ||
import org.apache.cassandra.utils.Hex; | ||
|
||
public class MutationVerificationUtils | ||
public class OwnershipVerificationUtils | ||
{ | ||
private static final boolean VERIFY_KEYS_ON_WRITE = Boolean.getBoolean("palantir_cassandra.verify_keys_on_write"); | ||
private static final Logger logger = LoggerFactory.getLogger(MutationVerificationUtils.class); | ||
private static final boolean VERIFY_KEYS_ON_READ = Boolean.getBoolean("palantir_cassandra.verify_keys_on_read"); | ||
private static final Logger logger = LoggerFactory.getLogger(OwnershipVerificationUtils.class); | ||
|
||
private static volatile Instant lastTokenRingCacheUpdate = Instant.MIN; | ||
|
||
private MutationVerificationUtils() | ||
private OwnershipVerificationUtils() | ||
{ | ||
} | ||
|
||
public static void verifyRead(Keyspace keyspace, ByteBuffer key) | ||
{ | ||
if (!VERIFY_KEYS_ON_READ) | ||
{ | ||
return; | ||
} | ||
verifyOperation(keyspace, key, ReadVerificationHandler.INSTANCE); | ||
} | ||
|
||
public static void verifyMutation(Mutation mutation) | ||
{ | ||
if (!VERIFY_KEYS_ON_WRITE) | ||
{ | ||
return; | ||
} | ||
verifyOperation(Keyspace.open(mutation.getKeyspaceName()), mutation.key(), MutationVerificationHandler.INSTANCE); | ||
} | ||
|
||
Keyspace keyspace = Keyspace.open(mutation.getKeyspaceName()); | ||
private static void verifyOperation(Keyspace keyspace, ByteBuffer key, OwnershipVerificationHandler handler) | ||
{ | ||
if (!(keyspace.getReplicationStrategy() instanceof NetworkTopologyStrategy)) | ||
{ | ||
return; | ||
} | ||
|
||
Token tk = StorageService.getPartitioner().getToken(mutation.key()); | ||
List<InetAddress> cachedNaturalEndpoints = StorageService.instance.getNaturalEndpoints(mutation.getKeyspaceName(), tk); | ||
Collection<InetAddress> pendingEndpoints = StorageService.instance.getTokenMetadata().pendingEndpointsFor(tk, mutation.getKeyspaceName()); | ||
String keyspaceName = keyspace.getName(); | ||
Token tk = StorageService.getPartitioner().getToken(key); | ||
List<InetAddress> cachedNaturalEndpoints = StorageService.instance.getNaturalEndpoints(keyspaceName, tk); | ||
Collection<InetAddress> pendingEndpoints = StorageService.instance.getTokenMetadata().pendingEndpointsFor(tk, keyspaceName); | ||
|
||
if (mutationIsInvalid(cachedNaturalEndpoints, pendingEndpoints)) | ||
if (operationIsInvalid(cachedNaturalEndpoints, pendingEndpoints)) | ||
{ | ||
if (cacheWasRecentlyRefreshed()) | ||
{ | ||
throwInvalidMutationException(mutation, keyspace, cachedNaturalEndpoints, pendingEndpoints); | ||
handler.onViolation(keyspace, key, cachedNaturalEndpoints, pendingEndpoints); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. an invalid read won't throw an exception here so it will constantly trigger i think we should define a There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. good catch! To avoid depending on the presence of an exception for correct control flow, I just added some returns in the correct place. |
||
} | ||
|
||
refreshCache(); | ||
|
||
List<InetAddress> refreshedNaturalEndpoints = StorageService.instance.getNaturalEndpoints(mutation.getKeyspaceName(), tk); | ||
List<InetAddress> refreshedNaturalEndpoints = StorageService.instance.getNaturalEndpoints(keyspaceName, tk); | ||
|
||
if (mutationIsInvalid(refreshedNaturalEndpoints, pendingEndpoints)) | ||
if (operationIsInvalid(refreshedNaturalEndpoints, pendingEndpoints)) | ||
{ | ||
throwInvalidMutationException(mutation, keyspace, refreshedNaturalEndpoints, pendingEndpoints); | ||
handler.onViolation(keyspace, key, cachedNaturalEndpoints, pendingEndpoints); | ||
} | ||
else | ||
{ | ||
logger.warn("Ignoring InvalidMutation error detected using stale token ring cache. Error was originally detected for key {} in keyspace {}." | ||
+ " Cached owners {} and {}. Actual owners {} and {}", | ||
Hex.bytesToHex(mutation.key().array()), | ||
mutation.getKeyspaceName(), | ||
cachedNaturalEndpoints, | ||
pendingEndpoints, | ||
refreshedNaturalEndpoints, | ||
pendingEndpoints); | ||
logger.warn("Ignoring InvalidOwnership error detected using stale token ring cache. Error was originally detected for key {} in keyspace {}." | ||
+ " Cached owners {}. Actual owners {}. Pending owners (non-cached) {}.", | ||
UnsafeArg.of("key", Hex.bytesToHex(key.array())), | ||
SafeArg.of("keyspace", keyspaceName), | ||
SafeArg.of("cachedNaturalEndpoints", cachedNaturalEndpoints), | ||
SafeArg.of("refreshedNaturalEndpoints", refreshedNaturalEndpoints), | ||
SafeArg.of("pendingEndpoints", pendingEndpoints)); | ||
} | ||
} | ||
|
||
keyspace.metric.validMutations.inc(); | ||
} | ||
|
||
|
||
private static void throwInvalidMutationException(Mutation mutation, Keyspace keyspace, List<InetAddress> naturalEndpoints, Collection<InetAddress> pendingEndpoints) | ||
{ | ||
keyspace.metric.invalidMutations.inc(); | ||
logger.error("InvalidMutation! Cannot apply mutation as this host {} does not contain key {} in keyspace {}. Only hosts {} and {} do.", | ||
FBUtilities.getBroadcastAddress(), Hex.bytesToHex(mutation.key().array()), mutation.getKeyspaceName(), naturalEndpoints, pendingEndpoints); | ||
throw new InvalidMutationException(); | ||
handler.onValid(keyspace); | ||
} | ||
|
||
private static void refreshCache() | ||
|
@@ -116,7 +121,7 @@ private static boolean cacheWasRecentlyRefreshed() | |
return Duration.between(lastTokenRingCacheUpdate, Instant.now()).compareTo(Duration.ofMinutes(10)) < 0; | ||
} | ||
|
||
private static boolean mutationIsInvalid(List<InetAddress> naturalEndpoints, Collection<InetAddress> pendingEndpoints) | ||
private static boolean operationIsInvalid(List<InetAddress> naturalEndpoints, Collection<InetAddress> pendingEndpoints) | ||
{ | ||
return !naturalEndpoints.contains(FBUtilities.getBroadcastAddress()) && !pendingEndpoints.contains(FBUtilities.getBroadcastAddress()); | ||
} | ||
|
@@ -126,5 +131,4 @@ static void clearLastTokenRingCacheUpdate() | |
{ | ||
lastTokenRingCacheUpdate = Instant.MIN; | ||
} | ||
|
||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,58 @@ | ||
/* | ||
* 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 com.palantir.cassandra.utils; | ||
|
||
import com.palantir.logsafe.SafeArg; | ||
import com.palantir.logsafe.UnsafeArg; | ||
import org.apache.cassandra.db.Keyspace; | ||
import org.apache.cassandra.utils.FBUtilities; | ||
import org.apache.cassandra.utils.Hex; | ||
import org.slf4j.Logger; | ||
import org.slf4j.LoggerFactory; | ||
|
||
import java.net.InetAddress; | ||
import java.nio.ByteBuffer; | ||
import java.util.Collection; | ||
import java.util.List; | ||
|
||
public class ReadVerificationHandler implements OwnershipVerificationHandler | ||
{ | ||
private static final Logger logger = LoggerFactory.getLogger(OwnershipVerificationUtils.class); | ||
|
||
public static final OwnershipVerificationHandler INSTANCE = new MutationVerificationHandler(); | ||
|
||
@Override | ||
public void onViolation(Keyspace keyspace, ByteBuffer key, List<InetAddress> naturalEndpoints, Collection<InetAddress> pendingEndpoints) | ||
{ | ||
keyspace.metric.invalidReads.inc(); | ||
logger.error( | ||
"Executed InvalidRead! This host {} does not contain key {} in keyspace {}. Only hosts {} and {} do.", | ||
SafeArg.of("address", FBUtilities.getBroadcastAddress()), | ||
UnsafeArg.of("key", Hex.bytesToHex(key.array())), | ||
SafeArg.of("keyspace", keyspace.getName()), | ||
SafeArg.of("naturalEndpoints", naturalEndpoints), | ||
SafeArg.of("pendingEndpoints", pendingEndpoints)); | ||
} | ||
|
||
@Override | ||
public void onValid(Keyspace keyspace) | ||
{ | ||
keyspace.metric.validReads.inc(); | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -21,6 +21,7 @@ | |
|
||
import com.google.common.util.concurrent.Uninterruptibles; | ||
|
||
import com.palantir.cassandra.utils.OwnershipVerificationUtils; | ||
import org.apache.cassandra.config.DatabaseDescriptor; | ||
import org.apache.cassandra.exceptions.IsBootstrappingException; | ||
import org.apache.cassandra.net.IVerbHandler; | ||
|
@@ -49,6 +50,8 @@ public void doVerb(MessageIn<ReadCommand> message, int id) | |
ReadResponse.serializer); | ||
Tracing.trace("Enqueuing response to {}", message.from); | ||
MessagingService.instance().sendReply(reply, id, message.from); | ||
|
||
OwnershipVerificationUtils.verifyRead(keyspace, command.key); | ||
} | ||
Comment on lines
51
to
55
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. note that this verification is intentionally after enqueuing the reply in order to avoid introducing additional latency on the read path. We should consider moving it earlier and throwing an exception after understanding in what circumstances this violation is logged, if any. |
||
|
||
public static ReadResponse getResponse(ReadCommand command, Row row) | ||
|
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 wouldn't this be of type
MutationVerificationHandler
and similarly forReadVerificationHandler
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.
discussed offline, I'm just following the
List l = new ArrayList();
pattern where you always use the most generic interface where possible.