DatoRLongAdder.java

  1. /*
  2.  * GovWay - A customizable API Gateway
  3.  * https://govway.org
  4.  *
  5.  * Copyright (c) 2005-2025 Link.it srl (https://link.it).
  6.  *
  7.  * This program is free software: you can redistribute it and/or modify
  8.  * it under the terms of the GNU General Public License version 3, as published by
  9.  * the Free Software Foundation.
  10.  *
  11.  * This program is distributed in the hope that it will be useful,
  12.  * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13.  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  14.  * GNU General Public License for more details.
  15.  *
  16.  * You should have received a copy of the GNU General Public License
  17.  * along with this program.  If not, see <http://www.gnu.org/licenses/>.
  18.  *
  19.  */

  20. package org.openspcoop2.pdd.core.controllo_traffico.policy.driver.redisson.counters;

  21. import java.util.concurrent.CompletionStage;

  22. import org.openspcoop2.pdd.config.OpenSPCoop2Properties;
  23. import org.openspcoop2.pdd.logger.OpenSPCoop2Logger;
  24. import org.openspcoop2.utils.Utilities;
  25. import org.openspcoop2.utils.UtilsRuntimeException;
  26. import org.redisson.api.RLongAdder;
  27. import org.redisson.api.RedissonClient;
  28. import org.slf4j.Logger;

  29. /**
  30.  * DatoRLongAdder
  31.  *
  32.  * @author Poli Andrea (apoli@link.it)
  33.  * @author $Author$
  34.  * @version $Rev$, $Date$
  35.  */

  36. public class DatoRLongAdder {

  37.     private RedissonClient redisson;
  38.     private String name;
  39.    
  40.     private RLongAdder counter;
  41.    
  42.     private int failover = -1;
  43.     public void setFailover(int failover) {
  44.         this.failover = failover;
  45.     }
  46.     private int failoverCheckEveryMs = -1;
  47.    
  48.     private Logger logControlloTraffico;
  49.    
  50.     public DatoRLongAdder(RedissonClient redisson, String name) {
  51.         this.redisson = redisson;
  52.         this.name = name;
  53.         this.initCounter();
  54.         OpenSPCoop2Properties op2Props = OpenSPCoop2Properties.getInstance();
  55.         this.failover = -1; // da gestire in futuro se serve
  56.         this.failoverCheckEveryMs = -1;
  57.         this.logControlloTraffico = OpenSPCoop2Logger.getLoggerOpenSPCoopControlloTraffico(op2Props.isControlloTrafficoDebug());
  58.     }
  59.     private void initCounter() {
  60.         this.counter = this.redisson.getLongAdder(this.name);
  61.     }
  62.    
  63.     public long sum() {
  64.         RLongAdderResponse r = process(RLongAdderOperation.SUM, -1, -1);
  65.         return r!=null ? r.valueL : -1; // else non dovrebbe succedere mai
  66.     }
  67.     public void add(long value) {
  68.         process(RLongAdderOperation.ADD, value, -1);
  69.     }
  70.     public void increment() {
  71.         process(RLongAdderOperation.INCREMENT, -1, -1);
  72.     }
  73.     public void decrement() {
  74.         process(RLongAdderOperation.DECREMENT, -1, -1);
  75.     }
  76.     public void reset() {
  77.         process(RLongAdderOperation.RESET, -1, -1);
  78.     }
  79.     public void destroy() {
  80.         process(RLongAdderOperation.DESTROY, -1, -1);
  81.     }
  82.    
  83.     private RLongAdderResponse process(RLongAdderOperation op, long arg1, long arg2) {
  84.         String prefix = "[Redis-RAtomicLong-"+this.name+" operation:"+op+"] ";
  85.         if(this.failover>0) {
  86.             return processFailOver(prefix, op, arg1, arg2);
  87.         }
  88.         else {
  89.             return operation(prefix, op, arg1, arg2);
  90.         }
  91.     }
  92.     private RLongAdderResponse processFailOver(String prefix, RLongAdderOperation op, long arg1, long arg2) {
  93.         boolean success = false;
  94.         Exception eFinal = null; // capire l'eccezione
  95.         RLongAdderResponse v = null;
  96.         for (int i = 0; i < this.failover; i++) {
  97.             try {
  98.                 if(i>0 && this.failoverCheckEveryMs>0) {
  99.                     Utilities.sleep(this.failoverCheckEveryMs);
  100.                     initCounter();
  101.                 }
  102.                 v = operation(prefix, op, arg1, arg2);
  103.                 success=true;
  104.                 break;
  105.             } catch (Exception e) {
  106.                 eFinal = e;
  107.                 if(i==0) {
  108.                     this.logControlloTraffico.error(prefix+"rilevato contatore distrutto (verrà riprovata la creazione): "+e.getMessage(),e);
  109.                 }
  110.                 else {
  111.                     this.logControlloTraffico.error(prefix+"il tenativo i="+i+" di ricreare il contatore è fallito: "+e.getMessage(),e);
  112.                 }
  113.             }
  114.         }
  115.         if(!success) {
  116.             throwDistributedObjectDestroyedException(prefix, eFinal);
  117.         }
  118.         return v;
  119.     }
  120.     private void throwDistributedObjectDestroyedException(String prefix, Exception eFinal) {
  121.         String msg = prefix+"tutti i tentativi di ricreare il contatore sono falliti";
  122.         this.logControlloTraffico.error(msg);
  123.         if(eFinal!=null) {
  124.             throw new UtilsRuntimeException(eFinal);
  125.         }
  126.         else {
  127.             throw new UtilsRuntimeException("tutti i tentativi di ricreare il contatore sono falliti"); // l'eccezione eFinal esiste
  128.         }
  129.     }
  130.    
  131.     private RLongAdderResponse operation(String prefix, RLongAdderOperation op, long arg1, long arg2){
  132.         switch (op) {
  133.         case SUM:
  134.             return new RLongAdderResponse(this.counter.sum());
  135.         case ADD:
  136.             this.counter.add(arg1);
  137.             return null;
  138.         case INCREMENT:
  139.             this.counter.increment();
  140.             return null;
  141.         case DECREMENT:
  142.             this.counter.decrement();
  143.             return null;
  144.         case RESET:
  145.             this.counter.reset();
  146.             return null;
  147.         case DESTROY:
  148.             try {
  149.                 this.counter.destroy();
  150.             }catch(Throwable e) {
  151.                 this.logControlloTraffico.error(prefix+"delete non riuscito: "+e.getMessage(),e);
  152.                 throw e;
  153.             }
  154.             return null;        
  155.         }
  156.         return null;
  157.     }
  158. }

  159. enum RLongAdderOperation {
  160.     SUM,
  161.     ADD, INCREMENT, DECREMENT,
  162.     RESET,
  163.     DESTROY
  164. }

  165. class RLongAdderResponse{
  166.     RLongAdderResponse(long l){
  167.         this.valueL = l;
  168.     }
  169.     RLongAdderResponse(boolean b){
  170.         this.valueB = b;
  171.     }
  172.     RLongAdderResponse(CompletionStage<Long> v){
  173.         this.valueAsync = v;
  174.     }
  175.     long valueL;    
  176.     boolean valueB;
  177.     CompletionStage<Long> valueAsync;
  178. }