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
HDDS-1499. OzoneManager Cache. #798
Changes from 7 commits
3479a57
2aa8432
b6b5595
7d25984
a08d166
7c5c481
a3ccf35
7349675
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 |
---|---|---|
|
@@ -21,8 +21,10 @@ | |
|
||
import java.io.IOException; | ||
|
||
import org.apache.commons.lang3.NotImplementedException; | ||
import org.apache.hadoop.classification.InterfaceStability; | ||
|
||
import org.apache.hadoop.utils.db.cache.CacheKey; | ||
import org.apache.hadoop.utils.db.cache.CacheValue; | ||
/** | ||
* Interface for key-value store that stores ozone metadata. Ozone metadata is | ||
* stored as key value pairs, both key and value are arbitrary byte arrays. Each | ||
|
@@ -97,6 +99,28 @@ void putWithBatch(BatchOperation batch, KEY key, VALUE value) | |
*/ | ||
String getName() throws IOException; | ||
|
||
/** | ||
* Add entry to the table cache. | ||
* | ||
* If the cacheKey already exists, it will override the entry. | ||
* @param cacheKey | ||
* @param cacheValue | ||
*/ | ||
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. well, I was really hoping that the fact that there is a cache is not visible to the layer that is reading and writing. 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. Once after the operation is executed in applyTransaction just before releasing the lock and sending a response to the client we need to add the response into cache. So that next subsequent read/write requests validation can be done with cache/db data. 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. thx makes sense. |
||
default void addCacheEntry(CacheKey<KEY> cacheKey, | ||
CacheValue<VALUE> cacheValue) { | ||
throw new NotImplementedException("addCacheEntry is not implemented"); | ||
} | ||
|
||
/** | ||
* Removes all the entries from the table cache which are having epoch value | ||
* less | ||
* than or equal to specified epoch value. | ||
* @param epoch | ||
*/ | ||
default void cleanupCache(long epoch) { | ||
throw new NotImplementedException("cleanupCache is not implemented"); | ||
} | ||
|
||
/** | ||
* Class used to represent the key and value pair of a db entry. | ||
*/ | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -20,6 +20,12 @@ | |
|
||
import java.io.IOException; | ||
|
||
import com.google.common.annotations.VisibleForTesting; | ||
import org.apache.hadoop.utils.db.cache.CacheKey; | ||
import org.apache.hadoop.utils.db.cache.CacheValue; | ||
import org.apache.hadoop.utils.db.cache.PartialTableCache; | ||
import org.apache.hadoop.utils.db.cache.TableCache; | ||
|
||
/** | ||
* Strongly typed table implementation. | ||
* <p> | ||
|
@@ -31,13 +37,16 @@ | |
*/ | ||
public class TypedTable<KEY, VALUE> implements Table<KEY, VALUE> { | ||
|
||
private Table<byte[], byte[]> rawTable; | ||
private final Table<byte[], byte[]> rawTable; | ||
|
||
private final CodecRegistry codecRegistry; | ||
|
||
private CodecRegistry codecRegistry; | ||
private final Class<KEY> keyType; | ||
arp7 marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
private Class<KEY> keyType; | ||
private final Class<VALUE> valueType; | ||
|
||
private final TableCache<CacheKey<KEY>, CacheValue<VALUE>> cache; | ||
|
||
private Class<VALUE> valueType; | ||
|
||
public TypedTable( | ||
Table<byte[], byte[]> rawTable, | ||
|
@@ -47,6 +56,7 @@ public TypedTable( | |
this.codecRegistry = codecRegistry; | ||
this.keyType = keyType; | ||
this.valueType = valueType; | ||
cache = new PartialTableCache<>(); | ||
} | ||
|
||
@Override | ||
|
@@ -69,8 +79,34 @@ public boolean isEmpty() throws IOException { | |
return rawTable.isEmpty(); | ||
} | ||
|
||
/** | ||
* Returns the value mapped to the given key in byte array or returns null | ||
arp7 marked this conversation as resolved.
Show resolved
Hide resolved
|
||
* if the key is not found. | ||
* | ||
* Caller's of this method should use synchronization mechanism, when | ||
* accessing. First it will check from cache, if it has entry return the | ||
* value, otherwise get from the RocksDB table. | ||
* | ||
* @param key metadata key | ||
* @return VALUE | ||
* @throws IOException | ||
*/ | ||
@Override | ||
public VALUE get(KEY key) throws IOException { | ||
// Here the metadata lock will guarantee that cache is not updated for same | ||
// key during get key. | ||
CacheValue< VALUE > cacheValue = cache.get(new CacheKey<>(key)); | ||
if (cacheValue == null) { | ||
// If no cache for the table or if it does not exist in cache get from | ||
// RocksDB table. | ||
return getFromTable(key); | ||
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. Not sure if you need this get again ? 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. For tables where the cache is disabled, we need to do as before just read from DB and return data. 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. Understood the comment, updated the code to remove getTable in multiple places. |
||
} else { | ||
// We have a value in cache, return the value. | ||
return cacheValue.getValue(); | ||
arp7 marked this conversation as resolved.
Show resolved
Hide resolved
|
||
} | ||
} | ||
|
||
private VALUE getFromTable(KEY key) throws IOException { | ||
byte[] keyBytes = codecRegistry.asRawData(key); | ||
byte[] valueBytes = rawTable.get(keyBytes); | ||
return codecRegistry.asObject(valueBytes, valueType); | ||
|
@@ -106,6 +142,40 @@ public void close() throws Exception { | |
|
||
} | ||
|
||
@Override | ||
public void addCacheEntry(CacheKey<KEY> cacheKey, | ||
CacheValue<VALUE> cacheValue) { | ||
// This will override the entry if there is already entry for this key. | ||
cache.put(cacheKey, cacheValue); | ||
} | ||
|
||
|
||
@Override | ||
public void cleanupCache(long epoch) { | ||
arp7 marked this conversation as resolved.
Show resolved
Hide resolved
|
||
cache.cleanup(epoch); | ||
} | ||
|
||
@VisibleForTesting | ||
TableCache<CacheKey<KEY>, CacheValue<VALUE>> getCache() { | ||
return cache; | ||
} | ||
|
||
public Table<byte[], byte[]> getRawTable() { | ||
return rawTable; | ||
} | ||
|
||
public CodecRegistry getCodecRegistry() { | ||
return codecRegistry; | ||
} | ||
|
||
public Class<KEY> getKeyType() { | ||
return keyType; | ||
} | ||
|
||
public Class<VALUE> getValueType() { | ||
return valueType; | ||
} | ||
|
||
/** | ||
* Key value implementation for strongly typed tables. | ||
*/ | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,56 @@ | ||
/** | ||
* 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.hadoop.utils.db.cache; | ||
|
||
import java.util.Objects; | ||
|
||
/** | ||
* CacheKey for the RocksDB table. | ||
* @param <KEY> | ||
*/ | ||
public class CacheKey<KEY> { | ||
|
||
private final KEY key; | ||
|
||
public CacheKey(KEY key) { | ||
Objects.requireNonNull(key, "Key Should not be null in CacheKey"); | ||
this.key = key; | ||
} | ||
|
||
public KEY getKey() { | ||
return key; | ||
} | ||
|
||
@Override | ||
public boolean equals(Object o) { | ||
if (this == o) { | ||
return true; | ||
} | ||
if (o == null || getClass() != o.getClass()) { | ||
return false; | ||
} | ||
CacheKey<?> cacheKey = (CacheKey<?>) o; | ||
return Objects.equals(key, cacheKey.key); | ||
} | ||
|
||
@Override | ||
public int hashCode() { | ||
return Objects.hash(key); | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
/** | ||
* 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.hadoop.utils.db.cache; | ||
|
||
import com.google.common.base.Optional; | ||
|
||
/** | ||
* CacheValue for the RocksDB Table. | ||
* @param <VALUE> | ||
*/ | ||
public class CacheValue<VALUE> { | ||
|
||
private Optional<VALUE> value; | ||
// This value is used for evict entries from cache. | ||
// This value is set with ratis transaction context log entry index. | ||
private long epoch; | ||
|
||
public CacheValue(Optional<VALUE> value, long epoch) { | ||
this.value = value; | ||
this.epoch = epoch; | ||
} | ||
|
||
public VALUE getValue() { | ||
return value.orNull(); | ||
} | ||
|
||
public long getEpoch() { | ||
return epoch; | ||
} | ||
|
||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,74 @@ | ||
/* | ||
* 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.hadoop.utils.db.cache; | ||
|
||
import java.util.Objects; | ||
|
||
/** | ||
* Class used which describes epoch entry. This will be used during deletion | ||
* entries from cache for partial table cache. | ||
* @param <CACHEKEY> | ||
*/ | ||
public class EpochEntry<CACHEKEY> implements Comparable<CACHEKEY> { | ||
|
||
private long epoch; | ||
private CACHEKEY cachekey; | ||
|
||
EpochEntry(long epoch, CACHEKEY cachekey) { | ||
this.epoch = epoch; | ||
this.cachekey = cachekey; | ||
} | ||
|
||
public long getEpoch() { | ||
return epoch; | ||
} | ||
|
||
public CACHEKEY getCachekey() { | ||
return cachekey; | ||
} | ||
|
||
@Override | ||
public boolean equals(Object o) { | ||
if (this == o) { | ||
return true; | ||
} | ||
if (o == null || getClass() != o.getClass()) { | ||
return false; | ||
} | ||
EpochEntry<?> that = (EpochEntry<?>) o; | ||
return epoch == that.epoch && cachekey == that.cachekey; | ||
} | ||
|
||
@Override | ||
public int hashCode() { | ||
return Objects.hash(epoch, cachekey); | ||
} | ||
|
||
public int compareTo(Object o) { | ||
if(this.epoch == ((EpochEntry<?>)o).epoch) { | ||
return 0; | ||
} else if (this.epoch < ((EpochEntry<?>)o).epoch) { | ||
return -1; | ||
} else { | ||
return 1; | ||
} | ||
} | ||
|
||
} |
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.
Nit: Space only change?
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.
Committing this for now since Anu +1'ed.