TimerEventiThread.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.timers;


  21. import java.io.File;
  22. import java.io.FileInputStream;
  23. import java.sql.Connection;
  24. import java.util.Date;
  25. import java.util.List;

  26. import org.openspcoop2.core.controllo_traffico.driver.PolicyGroupByActiveThreadsType;
  27. import org.openspcoop2.pdd.config.DBTransazioniManager;
  28. import org.openspcoop2.pdd.config.OpenSPCoop2Properties;
  29. import org.openspcoop2.pdd.config.Resource;
  30. import org.openspcoop2.pdd.core.controllo_traffico.NotificatoreEventi;
  31. import org.openspcoop2.pdd.core.controllo_traffico.policy.driver.GestorePolicyAttive;
  32. import org.openspcoop2.pdd.core.handlers.HandlerException;
  33. import org.openspcoop2.utils.UtilsException;
  34. import org.openspcoop2.utils.date.DateManager;
  35. import org.openspcoop2.utils.threads.BaseThread;
  36. import org.slf4j.Logger;


  37. /**    
  38.  * TimerEventiThread
  39.  *
  40.  * @author Poli Andrea (poli@link.it)
  41.  * @author $Author$
  42.  * @version $Rev$, $Date$
  43.  */
  44. public class TimerEventiThread extends BaseThread{

  45.     private static TimerState STATE = TimerState.OFF; // abilitato in OpenSPCoop2Startup al momento dell'avvio
  46.    
  47.     public static TimerState getSTATE() {
  48.         return STATE;
  49.     }
  50.     public static void setSTATE(TimerState sTATE) {
  51.         STATE = sTATE;
  52.     }

  53.     public static final String ID_MODULO = "TimerEventi";
  54.        
  55.     /** Logger utilizzato per debug. */
  56.     private Logger log = null;
  57.    
  58.     /** Indicazione se deve essere effettuato il log delle query */
  59.     private boolean debug = false;  
  60.    
  61.    
  62.     private OpenSPCoop2Properties properties;
  63.    
  64.     /** NotificatoreEventi */
  65.     private NotificatoreEventi notificatoreEventi = null;
  66.    
  67.     /** LastInterval */
  68.     private Date lastInterval;
  69.    
  70.     /** ConnectionTimeout */
  71.     private int checkConnectionTimeoutEveryXTimes = 1;
  72.     private int offsetConnectionTimeoutEveryXTimes = 0;
  73.     private Date lastIntervalConnectionTimeout;
  74.    
  75.     /** RequestReadTimeout */
  76.     private int checkRequestReadTimeoutEveryXTimes = 1;
  77.     private int offsetRequestReadTimeoutEveryXTimes = 0;
  78.     private Date lastIntervalRequestReadTimeout;
  79.    
  80.     private int checkReadTimeoutEveryXTimes = 1;
  81.     private int offsetReadTimeoutEveryXTimes = 0;
  82.     private Date lastIntervalReadTimeout;
  83.    
  84.     /** Immagine */
  85.     @SuppressWarnings("unused")
  86.     private boolean forceCheckPrimoAvvio = false;
  87.    
  88.     private static boolean inizializzazioneAttiva = false;
  89.     public static boolean isInizializzazioneAttiva() {
  90.         return inizializzazioneAttiva;
  91.     }
  92.     public static void setInizializzazioneAttiva(boolean inizializzazioneAttiva) {
  93.         TimerEventiThread.inizializzazioneAttiva = inizializzazioneAttiva;
  94.     }
  95.    
  96.     /** Costruttore */
  97.     @SuppressWarnings("deprecation")
  98.     public TimerEventiThread(Logger log) throws Exception{
  99.    
  100.         this.log = log;
  101.    
  102.         this.properties = OpenSPCoop2Properties.getInstance();
  103.         this.setTimeout(this.properties.getEventiTimerIntervalSeconds());
  104.        
  105.         this.checkConnectionTimeoutEveryXTimes = this.properties.getEventiTimerIntervalConnectionTimeoutEveryXTimes();
  106.         this.checkRequestReadTimeoutEveryXTimes = this.properties.getEventiTimerIntervalRequestReadTimeoutEveryXTimes();
  107.         this.checkReadTimeoutEveryXTimes = this.properties.getEventiTimerIntervalReadTimeoutEveryXTimes();
  108.        
  109.         // Eventi per Controllo Traffico
  110.         if(this.properties.isControlloTrafficoEnabled()){
  111.             this.notificatoreEventi = NotificatoreEventi.getInstance();
  112.            
  113.             // Il meccanismo di ripristino dell'immagine degli eventi non sembra funzionare
  114.             // Lascio comunque il codice se in futuro si desidera approfindire la questione
  115.             if(inizializzazioneAttiva) {
  116.                 List<PolicyGroupByActiveThreadsType> tipiGestorePolicyRateLimiting = null;
  117.                 try{
  118.                     tipiGestorePolicyRateLimiting = GestorePolicyAttive.getTipiGestoriAttivi();
  119.                 }catch(Throwable e){
  120.                     this.log.error("Errore durante l'inizializzazione dell'immagine degli eventi per il Controllo del Traffico: "+e.getMessage(),e);
  121.                 }
  122.                 if(tipiGestorePolicyRateLimiting!=null && !tipiGestorePolicyRateLimiting.isEmpty()) {
  123.                     for (PolicyGroupByActiveThreadsType type : tipiGestorePolicyRateLimiting) {
  124.                         File fDati = null;
  125.                         try{
  126.                             File fRepository = this.properties.getControlloTrafficoGestorePolicyFileSystemRecoveryRepository();
  127.                             if(fRepository!=null){
  128.                                 if(fRepository.exists()==false){
  129.                                     throw new Exception("Directory ["+fRepository.getAbsolutePath()+"] not exists");
  130.                                 }
  131.                                 if(fRepository.isDirectory()==false){
  132.                                     throw new Exception("File ["+fRepository.getAbsolutePath()+"] is not directory");
  133.                                 }
  134.                                 if(fRepository.canRead()==false){
  135.                                     throw new Exception("File ["+fRepository.getAbsolutePath()+"] cannot read");
  136.                                 }
  137.                                 if(fRepository.canWrite()==false){
  138.                                     throw new Exception("File ["+fRepository.getAbsolutePath()+"] cannot write");
  139.                                 }
  140.                                 fDati = new File(fRepository, GestorePolicyAttive.getControlloTrafficoEventiImage(type));
  141.                                 if(fDati.exists() && fDati.canRead() && fDati.length()>0){
  142.                                     FileInputStream fin = new FileInputStream(fDati);
  143.                                     this.notificatoreEventi.initialize(fin);
  144.                                     if(!fDati.delete()) {
  145.                                         // ignore
  146.                                     }
  147.                                     this.forceCheckPrimoAvvio = true;
  148.                                 }
  149.                             }
  150.                         }catch(Exception e){
  151.                             String img = null;
  152.                             if(fDati!=null){
  153.                                 img = fDati.getAbsolutePath();
  154.                             }
  155.                             throw new HandlerException("Inizializzazione dell'immagine degli eventi ["+img+"] per il Controllo del Traffico non riuscita: "+e.getMessage(),e);
  156.                         }
  157.                     }
  158.                 }
  159.             }
  160.         }
  161.        
  162.         this.lastInterval = DateManager.getDate();
  163.         this.lastIntervalConnectionTimeout = DateManager.getDate();
  164.         this.lastIntervalRequestReadTimeout = DateManager.getDate();
  165.         this.lastIntervalReadTimeout = DateManager.getDate();
  166.        
  167.         this.debug = this.properties.isEventiDebug();

  168.     }
  169.    
  170.    
  171.     @Override
  172.     public void process(){
  173.    
  174.         if(TimerState.ENABLED.equals(STATE)) {
  175.        
  176.             DBTransazioniManager dbManager = null;
  177.             Resource r = null;
  178.             try{
  179.                 dbManager = DBTransazioniManager.getInstance();
  180.                 r = dbManager.getResource(this.properties.getIdentitaPortaDefaultWithoutProtocol(), ID_MODULO, null);
  181.                 if(r==null){
  182.                     throw new UtilsException("Risorsa al database non disponibile");
  183.                 }
  184.                 Connection con = (Connection) r.getResource();
  185.                 if(con == null)
  186.                     throw new UtilsException("Connessione non disponibile");    
  187.                
  188.                 if(this.properties.isControlloTrafficoEnabled()){
  189.                    
  190.                     try{
  191.                         this.lastInterval = this.notificatoreEventi.process(this.log, this.getTimeout(), this.lastInterval, con, this.debug);
  192.                     }catch(Exception e){
  193.                         this.log.error("Errore durante la generazione degli eventi per il controllo del traffico: "+e.getMessage(),e);
  194.                     }
  195.                    
  196.                     // Comprensione offset per analisi connection timeout events
  197.                     try{
  198.                         boolean analyzeConnectionTimeout = isAnalyzeConnectionTimeout();
  199.                         if(analyzeConnectionTimeout) {
  200.                             this.lastIntervalConnectionTimeout = this.notificatoreEventi.processConnectionTimeout(this.log, (this.getTimeout()*this.checkConnectionTimeoutEveryXTimes), this.lastIntervalConnectionTimeout, con, this.debug);
  201.                         }
  202.                         else {
  203.                             this.notificatoreEventi.emitProcessConnectionTimeoutSkip(this.log, this.debug, this.offsetConnectionTimeoutEveryXTimes, this.checkConnectionTimeoutEveryXTimes);
  204.                         }
  205.                     }catch(Exception e){
  206.                         this.log.error("Errore durante la generazione degli eventi di connection timeout: "+e.getMessage(),e);
  207.                     }
  208.                    
  209.                     // Comprensione offset per analisi request read timeout events
  210.                     try {
  211.                         boolean analyzeRequestReadTimeout = isAnalyzeRequestReadTimeout();
  212.                         if(analyzeRequestReadTimeout) {
  213.                             this.lastIntervalRequestReadTimeout = this.notificatoreEventi.processRequestReadTimeout(this.log, (this.getTimeout()*this.checkRequestReadTimeoutEveryXTimes), this.lastIntervalRequestReadTimeout, con, this.debug);
  214.                         }
  215.                         else {
  216.                             this.notificatoreEventi.emitProcessRequestReadTimeoutSkip(this.log, this.debug, this.offsetRequestReadTimeoutEveryXTimes, this.checkRequestReadTimeoutEveryXTimes);
  217.                         }
  218.                     }catch(Exception e){
  219.                         this.log.error("Errore durante la generazione degli eventi di request read timeout: "+e.getMessage(),e);
  220.                     }
  221.                    
  222.                     // Comprensione offset per analisi read timeout events
  223.                     try {
  224.                         boolean analyzeReadTimeout = isAnalyzeReadTimeout();
  225.                         if(analyzeReadTimeout) {
  226.                             this.lastIntervalReadTimeout = this.notificatoreEventi.processReadTimeout(this.log, (this.getTimeout()*this.checkReadTimeoutEveryXTimes), this.lastIntervalReadTimeout, con, this.debug);
  227.                         }
  228.                         else {
  229.                             this.notificatoreEventi.emitProcessReadTimeoutSkip(this.log, this.debug, this.offsetReadTimeoutEveryXTimes, this.checkReadTimeoutEveryXTimes);
  230.                         }
  231.                     }catch(Exception e){
  232.                         this.log.error("Errore durante la generazione degli eventi di request read timeout: "+e.getMessage(),e);
  233.                     }
  234.                    
  235.                 }
  236.                                
  237.                 // Aggiungere in futuro altre gestione degli eventi
  238.                
  239.             }catch(Exception e){
  240.                 this.log.error("Errore durante la generazione degli eventi: "+e.getMessage(),e);
  241.             }finally{
  242.                 try{
  243.                     if(r!=null)
  244.                         dbManager.releaseResource(this.properties.getIdentitaPortaDefaultWithoutProtocol(), ID_MODULO, r);
  245.                 }catch(Exception eClose){
  246.                     // ignore
  247.                 }
  248.             }
  249.            
  250.         }
  251.         else {
  252.             this.log.info("Timer "+ID_MODULO+" disabilitato");
  253.         }
  254.                
  255.     }
  256.    
  257.     @Override
  258.     public void close(){
  259.         this.log.info("Thread per la generazione degli eventi terminato");
  260.     }
  261.            
  262.     private boolean isAnalyzeConnectionTimeout() {
  263.         this.offsetConnectionTimeoutEveryXTimes++;
  264.         boolean esito = false;
  265.         if(this.offsetConnectionTimeoutEveryXTimes==this.checkConnectionTimeoutEveryXTimes) {
  266.             /**System.out.println("CONNECTION TIMEOUT CHECK '"+this.offsetConnectionTimeoutEveryXTimes+"'=='"+this.checkConnectionTimeoutEveryXTimes+"' TRUE");*/
  267.             esito = true;
  268.             this.offsetConnectionTimeoutEveryXTimes = 0;
  269.         }
  270.         /**else {
  271.             System.out.println("CONNECTION TIMEOUT CHECK '"+this.offsetConnectionTimeoutEveryXTimes+"'<>'"+this.checkConnectionTimeoutEveryXTimes+"' FALSE");
  272.         }*/
  273.         return esito;
  274.     }
  275.     private boolean isAnalyzeRequestReadTimeout() {
  276.         this.offsetRequestReadTimeoutEveryXTimes++;
  277.         boolean esito = false;
  278.         if(this.offsetRequestReadTimeoutEveryXTimes==this.checkRequestReadTimeoutEveryXTimes) {
  279.             /**System.out.println("REQUEST READ TIMEOUT CHECK '"+this.offsetRequestReadTimeoutEveryXTimes+"'=='"+this.checkRequestReadTimeoutEveryXTimes+"' TRUE");*/
  280.             esito = true;
  281.             this.offsetRequestReadTimeoutEveryXTimes = 0;
  282.         }
  283.         /**else {
  284.             System.out.println("REQUEST READ TIMEOUT CHECK '"+this.offsetRequestReadTimeoutEveryXTimes+"'<>'"+this.checkRequestReadTimeoutEveryXTimes+"' FALSE");
  285.         }*/
  286.         return esito;
  287.     }
  288.     private boolean isAnalyzeReadTimeout() {
  289.         this.offsetReadTimeoutEveryXTimes++;
  290.         boolean esito = false;
  291.         if(this.offsetReadTimeoutEveryXTimes==this.checkReadTimeoutEveryXTimes) {
  292.             /**System.out.println("READ TIMEOUT CHECK '"+this.offsetReadTimeoutEveryXTimes+"'=='"+this.checkReadTimeoutEveryXTimes+"' TRUE");*/
  293.             esito = true;
  294.             this.offsetReadTimeoutEveryXTimes = 0;
  295.         }
  296.         else {
  297.             /**System.out.println("READ TIMEOUT CHECK '"+this.offsetReadTimeoutEveryXTimes+"'<>'"+this.checkReadTimeoutEveryXTimes+"' FALSE");*/
  298.         }
  299.         return esito;
  300.     }
  301. }