hi Ignite team,
I want to implement a customized BackupFilter which is called in the
RendezvousAffinityFunction for my own cluster's feature, and I encountered a
problem: how to get the logger in ignite?
I've tried below ways, but all of them don't work, (NullPointerException).
1. Use the way just like below in RendezvousAffinityFunction, but seems that
the log cannot be automatically injected in my code, but it works in
RendezvousAffinityFunction. Any extra work for this?
/** Logger instance. */
@LoggerResource
private transient IgniteLogger log;
2. JavaLogger log = new JavaLogger();
3. U.warn(null, msg)
4. Try to pass the GridKernelContext to my class, then use IgniteLogger log
= ctx.log(myclass). It works when create my class, but do marshal/unmarshal,
it becomes null again.
BTW, I hard-code my BackupFilter in the RendezvousAffinityFunction as
default not by config. Because i use the .net version, it's a little
complicated to use config for this now.
Any suggestion on this?
My detailed class is as below:
public class ScaleUnitBackFilter implements IgniteBiPredicate {
/**
* It's used by the JdkMarshaller
*/
private static final long serialVersionUID = -5036727407264096908L;
private static final long ReloadCheckIntervalInMilliSecond = 30;
/**
* delay the loading to the first read
*/
private long lastLoadTime = 0;
private static final String ScaleUnitFilePath =
"d:/data/machineinfo.csv";
private HashMap scaleUnitMap;
/** Logger instance. */
@LoggerResource
private transient IgniteLogger log;
public ScaleUnitBackFilter() {
scaleUnitMap = new HashMap();
}
@Override
public boolean apply(ClusterNode primaryNode, ClusterNode
backupNodeCandidate) {
long curTime = U.currentTimeMillis();
if (curTime - lastLoadTime >= ReloadCheckIntervalInMilliSecond)
{
loadScaleUnitMap();
}
A.ensure(primaryNode.hostNames().size() >= 1, "Primary Node
must have
hostname.");
A.ensure(backupNodeCandidate.hostNames().size() >= 1, "Backup
Node must
have hostname.");
// Remove the domain in the full hostname
String pn = primaryNode.hostNames().toArray(new
String[0])[0].split("\\.")[0];
String bnc = backupNodeCandidate.hostNames().toArray(new
String[0])[0].split("\\.")[0];
LT.info(log, "PN: " + pn + ", BNC: " + bnc, false);
if (scaleUnitMap == null || scaleUnitMap.isEmpty()) {
LT.warn(log, null, "The machineinfo.csv file may be
empty. !!!PAY MORE
ATTENTION!!!", false);
return true;
}
if (!scaleUnitMap.containsKey(primaryNode) ||
!scaleUnitMap.containsKey(backupNodeCandidate)) {
LT.warn(log, null, "One machine isn't in the
machineinfo.csv. !!!PAY MORE
ATTENTION!!!", false);
return true;
}
MachineInfo pnInfo = scaleUnitMap.get(primaryNode);
LT.info(log, printMachineInfo(pn, pnInfo), false);
MachineInfo bncInfo = scaleUnitMap.get(backupNodeCandidate);
LT.info(log, printMachineInfo(bnc, bncInfo), false);
// If in the same scale unit or backup node isn't in 'H'
status, don't
select it as the backup node
if (pnInfo.scaleUnit.equals(bncInfo.scaleUnit) ||
!"H".equals(bncInfo.status)) {
LT.info(log, "Backup Node Candidate is filtered!",
false);
return false;
}
LT.info(log, "PN: " + pn + ", BN: " + bnc + " is selected!",
false);
return true;
}
private String printMachineInfo(String machine, MachineInfo
machineInfo) {
return machine + "[" + machineInfo.scaleUnit + ", " +
machineInfo.status +
"]";
}
private synchronized void loadScaleUnitMap() {
// double check
long curTime = U.currentTimeMillis();
if (curTime - lastLoadTime >= ReloadCheckIntervalInMilliSecond)
{
return;
}
String line = null;
String csvSplitBy = ",";
BufferedReader br = null;
try {
br = new BufferedReader(new
FileReader(ScaleUnitFilePath));
while ((line = br.readLine()) != null) {
String[] fields = line.split(csvSplitBy);