Please wait. This can take some minutes ...
Many resources are needed to download a project. Please understand that we have to compensate our server costs. Thank you in advance.
Project price only 1 $
You can buy this project and download/modify it how often you want.
org.apache.flink.runtime.state.gemini.engine.page.PageStoreHashKListImpl Maven / Gradle / Ivy
/*
* 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.flink.runtime.state.gemini.engine.page;
import org.apache.flink.api.common.typeutils.TypeSerializer;
import org.apache.flink.api.java.tuple.Tuple2;
import org.apache.flink.core.memory.DataInputView;
import org.apache.flink.runtime.state.gemini.engine.GRegion;
import org.apache.flink.runtime.state.gemini.engine.exceptions.GeminiRuntimeException;
import org.apache.flink.runtime.state.gemini.engine.exceptions.GeminiShutDownException;
import org.apache.flink.runtime.state.gemini.engine.memstore.GSValue;
import org.apache.flink.runtime.state.gemini.engine.page.bmap.BinaryKey;
import org.apache.flink.runtime.state.gemini.engine.page.bmap.BinaryValue;
import org.apache.flink.runtime.state.gemini.engine.page.bmap.ByteBufferDataInputView;
import org.apache.flink.runtime.state.gemini.engine.page.bmap.GBinaryHashMap;
import org.apache.flink.runtime.state.gemini.engine.page.bmap.GBufferAddressMapping;
import org.apache.flink.runtime.state.gemini.engine.rm.GByteBuffer;
import org.apache.flink.shaded.netty4.io.netty.util.concurrent.EventExecutor;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import static org.apache.flink.runtime.state.gemini.engine.page.bmap.GBinaryHashMap.EMPTY_G_BINARY_HASHMAP;
/**
* PageStoreHashKListImpl.
*/
public class PageStoreHashKListImpl extends AbstractHashPageStore>> implements PageStoreKList {
protected PageSerdeFlinkListImpl pageSerdeFlinkListImpl;
public PageStoreHashKListImpl(
GRegion gRegion, EventExecutor eventExecutor) {
this(gRegion, null, eventExecutor);
}
public PageStoreHashKListImpl(
GRegion gRegion, PageIndex pageIndex, EventExecutor eventExecutor) {
super(DataPage.DataPageType.KList, gRegion, pageIndex, eventExecutor);
this.pageSerdeFlinkListImpl = (PageSerdeFlinkListImpl) gRegionContext.getPageSerdeFlink();
}
@Override
public boolean contains(K key) {
//GeminiDB define empty collection as null.
List> result = get(key);
return get(key) != null && !result.isEmpty();
}
@Override
long getRequestCount(List>>>> dataSet) {
return dataSet.stream().map((value) -> value.f1.getRequestCount()).reduce(0, (a, b) -> a + b);
}
@Override
DataPage>> createDataPage(
long version, List>>>> dataSet, int logicPageId) {
GBinaryHashMap gBinaryHashMap = GBinaryHashMap.of(
getDataPageType(),
dataSet,
this.pageSerdeFlink.getKeySerde(),
pageSerdeFlinkListImpl.getgListValueTypeSerializer(),
version,
logicPageId,
gContext.getSupervisor().getAllocator(),
1,
gRegionContext.getGContext().getInPageGCompressAlgorithm());
return gBinaryHashMap == EMPTY_G_BINARY_HASHMAP
? null
: new DataPageKListImpl<>(gBinaryHashMap,
pageSerdeFlinkListImpl.getValueSerde(),
pageSerdeFlinkListImpl.getgListValueTypeSerializer());
}
@Override
public List> get(K key) {
final PageIndexContext pageIndexContext = pageIndex.getPageIndexContext(key, false);
final LogicalPageChain logicPageID = pageIndexContext.getLogicalPageChain();
if (isNullPage(logicPageID)) {
return null;
}
List> finalResult = null;
int curIndex = logicPageID.getCurrentPageChainIndex();
List binaryValueReversedOrderList = new ArrayList<>();
Map fetchedDataPageMap = new HashMap<>();
List needToRelease = new ArrayList<>();
while (curIndex >= 0 && gContext.isDBNormal()) {
DataPage dataPage = getDataPageAutoLoadIfNeed(key, logicPageID, pageIndexContext.getPageIndexID(), curIndex, fetchedDataPageMap);
if (dataPage == null) {
curIndex--;
continue;
}
needToRelease.add(dataPage);
if (!(dataPage instanceof DataPageKList)) {
needToRelease.forEach(page -> page.release());
fetchedDataPageMap.values().forEach(page -> page.release());
throw new IllegalArgumentException("Internal BUG, error page");
}
dataPage.addRequestCount(cacheManager.getCurrentTickTime(), 1);
gRegionContext.getPageStoreStats().addPageRequestCount(1);
BinaryValue binaryValue = dataPage.getBinaryValue(key);
if (binaryValue != null) {
if (binaryValue.getGValueType() == GValueType.Delete) {
//old value is useless
break;
} else {
binaryValueReversedOrderList.add(binaryValue);
if (binaryValue.getGValueType() == GValueType.PutList) {
//old value is useless
break;
}
}
}
curIndex--;
}
if (!gContext.isDBNormal()) {
needToRelease.forEach(dataPage -> dataPage.release());
fetchedDataPageMap.values().forEach(dataPage -> dataPage.release());
throw new GeminiShutDownException("DB is in abnormal status " + gContext.getDBStatus().name());
}
if (binaryValueReversedOrderList.size() == 0) {
finalResult = null;
} else {
finalResult = doCompactValueToPOJO(binaryValueReversedOrderList);
}
needToRelease.forEach(dataPage -> dataPage.release());
tryLaunchCompactionByRead(pageIndexContext, logicPageID, fetchedDataPageMap);
// NOTE: state will be filtered by the upper level
return finalResult;
}
protected List> doCompactValueToPOJO(List binaryValueReversedOrderList) {
try {
List> result = new ArrayList<>();
int index = binaryValueReversedOrderList.size() - 1;
while (index >= 0) {
BinaryValue binaryValue = binaryValueReversedOrderList.get(index);
DataInputView byteBufferDataInputView = new ByteBufferDataInputView(binaryValue.getBb(),
binaryValue.getValueOffset(),
binaryValue.getValueLen());
List> gsValueList = pageSerdeFlinkListImpl.getgListValueTypeSerializer().deserialize(
byteBufferDataInputView);
if (gsValueList == null) {
continue;
}
result.addAll(gsValueList);
index--;
}
return result;
} catch (Exception e) {
throw new GeminiRuntimeException("Exception: " + e.getMessage(), e);
}
}
@Override
public void getAll(Map>>> container) {
// TODO
}
@Override
public DataPage doCompactPage(
PageIndexContext pageIndexContext, boolean isMajor, List canCompactPageListReversedOrder, long version, int logicPageId) {
if (canCompactPageListReversedOrder == null || canCompactPageListReversedOrder.size() == 0) {
throw new GeminiRuntimeException("Internal BUG");
}
return doCompactPageForStructureValue(pageIndexContext, isMajor, canCompactPageListReversedOrder, version, logicPageId);
}
@Override
protected DataPage doBuildDataPageFromGBinaryMap(
boolean isMajor,
long version,
int logicPageId,
TypeSerializer keySerde,
Map finalCompactedMap,
long compactionCount,
GBufferAddressMapping pageMapping) {
GBinaryHashMap gBinaryHashMap = GBinaryHashMap.ofBinaryMap(
getDataPageType(),
isMajor,
version,
logicPageId,
this.pageSerdeFlink.getKeySerde(),
gContext.getSupervisor().getAllocator(),
finalCompactedMap,
compactionCount,
gContext.getStateFilter(),
gRegionContext);
//TODO delReference finalCompactedMap.
return gBinaryHashMap == EMPTY_G_BINARY_HASHMAP
? null
: new DataPageKListImpl<>(gBinaryHashMap,
pageSerdeFlinkListImpl.getValueSerde(),
pageSerdeFlinkListImpl.getgListValueTypeSerializer());
}
@Override
DataPage>> boxDataPage(PageAddress pageAddress, GByteBuffer byteBuffer, int logicPageChainIndex, int logicPageChainHashCode) {
checkDataPageTypeToBox(byteBuffer);
GBinaryHashMap gBinaryHashMap = new GBinaryHashMap<>(byteBuffer,
this.pageSerdeFlink.getKeySerde(),
pageAddress.getChecksum());
return gBinaryHashMap == EMPTY_G_BINARY_HASHMAP
? null
: new DataPageKListImpl<>(gBinaryHashMap,
pageSerdeFlinkListImpl.getValueSerde(),
pageSerdeFlinkListImpl.getgListValueTypeSerializer());
}
@Override
BinaryValue doCompactValue(
List binaryValueList,
boolean isMajor,
long version,
int logicPageId,
GBufferAddressMapping pageMapping) {
return DataPageKListImpl.doCompactionKListValue(binaryValueList,
pageSerdeFlink.getValueSerde(),
isMajor,
version,
logicPageId,
//value use DefaultAllocator
gContext.getSupervisor().getDefaultAllocator());
}
}