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<ClusterNode,
ClusterNode> {
        /**
         * It's used by the JdkMarshaller
         */
        private static final long serialVersionUID = -5036727407264096908L;

        private static final long ReloadCheckIntervalInMilliSecond = 300000;
        /**
         * delay the loading to the first read
         */
        private long lastLoadTime = 0;
        
        private static final String ScaleUnitFilePath = 
"d:/data/machineinfo.csv";

        private HashMap<String, MachineInfo> scaleUnitMap;
        
        /** Logger instance. */
    @LoggerResource
    private transient IgniteLogger log;
        
        public ScaleUnitBackFilter() {
                scaleUnitMap = new HashMap<String, MachineInfo>();
        }
        
        @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);
                                // remove some comments
                                if (fields.length < 11) {
                                        continue;
                                }
                                
                                // remove header
                                if (fields[0].startsWith("#Fields")) {
                                        continue;
                                }
                                
                                String machine = fields[0];
                                String status = fields[10];
                                Integer scaleUnit = Integer.parseInt(fields[7]);
                                MachineInfo mi = new MachineInfo(scaleUnit, 
status);
                                
                                scaleUnitMap.put(machine, mi);
                        }
                        
                        lastLoadTime = curTime;
                } catch (FileNotFoundException e) {
                        LT.error(log, e, "MachinesInfo.csv doesn't exist!");
                } catch (IOException e) {
                        LT.error(log, e, "Failed to operate the 
MachinesInfo.csv!");
                } finally {
                        if (br != null) {
                                try {
                                        br.close();
                                } catch (IOException e) {
                                        LT.error(log, e, "Failed to close the 
MachinesInfo.csv!");
                                }
                        }
                }
                
        }
        
        class MachineInfo implements Serializable {
                /**
                 * It's used by the JdkMarshaller
                 */
                private static final long serialVersionUID = 
-656959122464042938L;
                
                private Integer scaleUnit;
                private String status;
                
                public MachineInfo(Integer su, String s) {
                        this.scaleUnit = su;
                        this.status = s;
                }
                
                public Integer getScaleUnit() {
                        return scaleUnit;
                }
                public void setScaleUnit(Integer scaleUnit) {
                        this.scaleUnit = scaleUnit;
                }
                public String getStatus() {
                        return status;
                }
                public void setStatus(String status) {
                        this.status = status;
                }
        }
}
Thanks,
-Jason



--
View this message in context: 
http://apache-ignite-users.70518.x6.nabble.com/How-to-use-the-logger-in-the-BackupFilter-tp6442.html
Sent from the Apache Ignite Users mailing list archive at Nabble.com.

Reply via email to