Repository navigation
SAMZA-2174: Throw a record too large exception for oversized records in changelog #1008
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
Merged
cameronlee314
merged 12 commits into
apache:master
from
PawasChhokra:changelog_large_message
Jul 31, 2019
Merged
Changes from all commits
Commits
Show all changes
12 commits
Select commit
Hold shift + click to select a range
43e6112
Throw a record too large exception for changelog oversized records
PawasChhokra 4e3b65e
Change implementation to handle large messages in CachedStore based o…
PawasChhokra 1cc818c
Merge branch 'master' into changelog_large_message
PawasChhokra 8fa0dab
Address review and change new Scala classes to Java
PawasChhokra 10e5c3c
Merge branch 'changelog_large_message' of https://github.com/PawasChh…
PawasChhokra 1ddcc62
Address review and add a test case
PawasChhokra 1ea72ff
Address review
PawasChhokra 95648ed
Address review
PawasChhokra e26a6ff
Address review
PawasChhokra 53fb9a5
Address review
PawasChhokra 723d6ff
Address review
PawasChhokra 2d8a583
Address review
PawasChhokra 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
150 changes: 150 additions & 0 deletions
150
samza-kv/src/main/java/org/apache/samza/storage/kv/LargeMessageSafeStore.java
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,150 @@ | ||
| /* | ||
| * 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.samza.storage.kv; | ||
|
|
||
| import java.util.ArrayList; | ||
| import java.util.List; | ||
| import org.apache.samza.metrics.MetricsRegistryMap; | ||
| import org.slf4j.Logger; | ||
| import org.slf4j.LoggerFactory; | ||
|
|
||
|
|
||
| /** | ||
| * This class checks for the size of the message being stored and either throws an exception when a large message is | ||
| * encountered or ignores the large message and continues processing, depending on how it is configured. | ||
| */ | ||
| public class LargeMessageSafeStore implements KeyValueStore<byte[], byte[]> { | ||
|
PawasChhokra marked this conversation as resolved.
|
||
|
|
||
| private static final Logger LOG = LoggerFactory.getLogger(LargeMessageSafeStore.class); | ||
| private final KeyValueStore<byte[], byte[]> store; | ||
| private final String storeName; | ||
| private final boolean dropLargeMessages; | ||
| private final int maxMessageSize; | ||
| private final LargeMessageSafeStoreMetrics largeMessageSafeStoreMetrics; | ||
|
|
||
| public LargeMessageSafeStore(KeyValueStore<byte[], byte[]> store, String storeName, boolean dropLargeMessages, int maxMessageSize) { | ||
| this.store = store; | ||
| this.storeName = storeName; | ||
| this.dropLargeMessages = dropLargeMessages; | ||
| this.maxMessageSize = maxMessageSize; | ||
| this.largeMessageSafeStoreMetrics = new LargeMessageSafeStoreMetrics(storeName, new MetricsRegistryMap()); | ||
| } | ||
|
|
||
| @Override | ||
| public byte[] get(byte[] key) { | ||
| return store.get(key); | ||
| } | ||
|
|
||
| /** | ||
| * This function puts a message in the store after validating its size. | ||
| * It drops the large message if it has been configured to do so. | ||
| * Otherwise, it throws an exception stating that a large message was encountered. | ||
| * | ||
| * @param key the key with which the specified {@code value} is to be associated. | ||
| * @param value the value with which the specified {@code key} is to be associated. | ||
| */ | ||
| @Override | ||
| public void put(byte[] key, byte[] value) { | ||
|
PawasChhokra marked this conversation as resolved.
|
||
| validateMessageSize(value); | ||
| if (!isLargeMessage(value)) { | ||
|
PawasChhokra marked this conversation as resolved.
|
||
| store.put(key, value); | ||
| } else { | ||
| LOG.info("Ignoring a large message with size " + value.length + " since it is greater than " | ||
| + "the maximum allowed value of " + maxMessageSize); | ||
| largeMessageSafeStoreMetrics.ignoredLargeMessages().inc(); | ||
| } | ||
| } | ||
|
|
||
| /** | ||
| * This function puts messages in the store after validating their size. | ||
| * It drops any large messages in the entry list if it has been configured to do so. | ||
| * Otherwise, it throws an exception stating that a large message was encountered, | ||
| * and does not put any of the messages in the store. | ||
| * | ||
| * @param entries the updated mappings to put into this key-value store. | ||
| */ | ||
| @Override | ||
| public void putAll(List<Entry<byte[], byte[]>> entries) { | ||
| entries.forEach(entry -> { | ||
| validateMessageSize(entry.getValue()); | ||
| }); | ||
| List<Entry<byte[], byte[]>> largeMessageSafeEntries = removeLargeMessages(entries); | ||
| store.putAll(largeMessageSafeEntries); | ||
| } | ||
|
|
||
| @Override | ||
| public void delete(byte[] key) { | ||
| store.delete(key); | ||
| } | ||
|
|
||
| @Override | ||
| public void deleteAll(List<byte[]> keys) { | ||
| store.deleteAll(keys); | ||
| } | ||
|
|
||
| @Override | ||
| public KeyValueIterator<byte[], byte[]> range(byte[] from, byte[] to) { | ||
| return store.range(from, to); | ||
| } | ||
|
|
||
| @Override | ||
| public KeyValueSnapshot<byte[], byte[]> snapshot(byte[] from, byte[] to) { | ||
| return store.snapshot(from, to); | ||
| } | ||
|
|
||
| @Override | ||
| public KeyValueIterator<byte[], byte[]> all() { | ||
| return store.all(); | ||
| } | ||
|
|
||
| @Override | ||
| public void close() { | ||
| store.close(); | ||
| } | ||
|
|
||
| @Override | ||
| public void flush() { | ||
| store.flush(); | ||
| } | ||
|
|
||
| private void validateMessageSize(byte[] message) { | ||
| if (!dropLargeMessages && isLargeMessage(message)) { | ||
| throw new RecordTooLargeException("The message size " + message.length + " for store " + storeName | ||
| + " was larger than the maximum allowed message size " + maxMessageSize + "."); | ||
| } | ||
| } | ||
|
|
||
| private boolean isLargeMessage(byte[] message) { | ||
| return message != null && message.length > maxMessageSize; | ||
| } | ||
|
|
||
| private List<Entry<byte[], byte[]>> removeLargeMessages(List<Entry<byte[], byte[]>> entries) { | ||
| List<Entry<byte[], byte[]>> largeMessageSafeEntries = new ArrayList<>(); | ||
| entries.forEach(entry -> { | ||
| if (!isLargeMessage(entry.getValue())) { | ||
| largeMessageSafeEntries.add(entry); | ||
| } else { | ||
| LOG.info("Ignoring a large message with size " + entry.getValue().length + " since it is greater than " | ||
| + "the maximum allowed value of " + maxMessageSize); | ||
| largeMessageSafeStoreMetrics.ignoredLargeMessages().inc(); | ||
| } | ||
| }); | ||
| return largeMessageSafeEntries; | ||
| } | ||
| } | ||
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.
Uh oh!
There was an error while loading. Please reload this page.