/
InfinispanLofState.java
175 lines (150 loc) · 6.28 KB
/
InfinispanLofState.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
/**
* Copyright (c) Acroquest Technology Co, Ltd. All Rights Reserved.
* Please read the associated COPYRIGHTS file for more details.
*
* THE SOFTWARE IS PROVIDED BY Acroquest Technolog Co., Ltd.,
* WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING
* BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDER BE LIABLE FOR ANY
* CLAIM, DAMAGES SUFFERED BY LICENSEE AS A RESULT OF USING, MODIFYING
* OR DISTRIBUTING THIS SOFTWARE OR ITS DERIVATIVES.
*/
package acromusashi.stream.ml.anomaly.lof.state;
import java.io.IOException;
import java.util.Arrays;
import java.util.concurrent.TimeUnit;
import org.infinispan.client.hotrod.RemoteCache;
import org.infinispan.client.hotrod.RemoteCacheManager;
import org.infinispan.client.hotrod.configuration.Configuration;
import org.infinispan.client.hotrod.configuration.ConfigurationBuilder;
import org.infinispan.commons.marshall.Marshaller;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import acromusashi.stream.ml.anomaly.lof.entity.LofDataSet;
import com.google.common.base.Joiner;
/**
* LOF分散用MapStateの保存先にInfinispanを用いたクラス<br>
* 設定された「parallelismHint」の数だけ存在する。
*
* @author kimura
*/
public class InfinispanLofState extends LofState
{
/** キャッシュ上のデータ生存期間のデフォルト値 */
private static final int DEFAULT_LIFESPAN = 600;
/** logger */
private static final Logger logger = LoggerFactory.getLogger(InfinispanLofState.class);
/** キャッシュ上のデータ生存期間(単位:秒) */
protected int lifespan = DEFAULT_LIFESPAN;
/** 投入先のサーバ情報 */
protected String targetServer;
/** 投入先キャッシュ名称 */
protected String cacheName;
/** 状態を保存するベースキー */
protected String baseKey;
/** Remoteキャッシュマネージャ */
protected transient RemoteCacheManager clientManager;
/** 状態保存用キャッシュ */
protected transient RemoteCache<String, LofDataSet> stateCache;
/** TransationId保存用キャッシュ */
protected transient RemoteCache<String, Long> txIdCache;
/**
* 設定値、構成情報をパラメータとしてインスタンスを生成する。
*
* @param targetServer 投入先のサーバ情報
* @param cacheName 投入先キャッシュ名称
* @param partitionIndex 何番目かのStateかを示すインデックス
* @param numPartitions Stateの数
*/
public InfinispanLofState(String targetServer, String cacheName, int partitionIndex,
int numPartitions)
{
super(partitionIndex, numPartitions);
this.targetServer = targetServer;
this.cacheName = cacheName;
}
/**
* {@inheritDoc}
*/
@Override
protected void onInitialize()
{
ConfigurationBuilder builder = new ConfigurationBuilder();
ClassLoader loader = Thread.currentThread().getContextClassLoader();
Configuration config = builder.classLoader(loader).addServers(this.targetServer).build();
this.clientManager = new RemoteCacheManager(config, true);
this.stateCache = this.clientManager.getCache(this.cacheName);
this.txIdCache = this.clientManager.getCache(this.cacheName);
}
/**
* {@inheritDoc}
*/
@Override
protected LofDataSet getState(String baseKey, Long txId, int partitionIndex,
boolean isSelfPartition) throws IOException
{
String targetKey = null;
// 指定された条件のクラスタ状態を取得する。取得結果がnull、または不正だった場合はnullとして扱う。
// 自分自身のパーティションと異なるパーティションの値を取得する場合、最新の状態値保存TransatctionIdを取得し用いる。
if (isSelfPartition == true)
{
targetKey = Joiner.on("_").skipNulls().join(
Arrays.asList(baseKey, txId, partitionIndex));
}
else
{
String txIdKey = Joiner.on("_").skipNulls().join(Arrays.asList(baseKey, partitionIndex));
Long anotherTxId = this.txIdCache.get(txIdKey);
targetKey = Joiner.on("_").skipNulls().join(
Arrays.asList(baseKey, anotherTxId, partitionIndex));
}
LofDataSet targetValue = this.stateCache.get(targetKey);
return targetValue;
}
/**
* {@inheritDoc}
*/
@Override
protected void putState(String baseKey, Long txId, int partitionIndex, LofDataSet putState)
throws IOException
{
this.baseKey = baseKey;
String putKey = Joiner.on("_").skipNulls().join(
Arrays.asList(baseKey, txId, partitionIndex));
// ログレベルが「DEBUG」の場合は格納するオブジェクトの予測サイズを出力
if (logger.isDebugEnabled() == true)
{
Marshaller marchaller = this.clientManager.getMarshaller();
int marchalledSize = 0;
try
{
marchalledSize = marchaller.objectToByteBuffer(putState).length;
}
catch (InterruptedException ex)
{
logger.warn("Object Marchall failed.", ex);
}
logger.debug("Infinispan put. Key=" + putKey + ", Size=" + marchalledSize);
}
this.stateCache.put(putKey, putState, this.lifespan, TimeUnit.SECONDS);
}
/**
* {@inheritDoc}
*/
@Override
public void commit(Long txid)
{
// commit時に自パーティションの最終保存TransactionIdを保存する。
String putKey = Joiner.on("_").skipNulls().join(
Arrays.asList(this.baseKey, this.partitionIndex));
this.txIdCache.put(putKey, txid, this.lifespan, TimeUnit.SECONDS);
}
/**
* @param lifespan the lifespan to set
*/
public void setLifespan(int lifespan)
{
this.lifespan = lifespan;
}
}