View Javadoc

    1   /**
    2    * Copyright (c) 2002-2011 "Neo Technology,"
    3    * Network Engine for Objects in Lund AB [http://neotechnology.com]
    4    *
    5    * This file is part of Neo4j.
    6    *
    7    * Neo4j is free software: you can redistribute it and/or modify
    8    * it under the terms of the GNU General Public License as published by
    9    * the Free Software Foundation, either version 3 of the License, or
    10   * (at your option) any later version.
    11   *
    12   * This program is distributed in the hope that it will be useful,
    13   * but WITHOUT ANY WARRANTY; without even the implied warranty of
    14   * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    15   * GNU General Public License for more details.
    16   *
    17   * You should have received a copy of the GNU General Public License
    18   * along with this program.  If not, see <http://www.gnu.org/licenses/>.
    19   */
    20  package org.neo4j.server.rrd;
    21
    22  import java.io.File;
    23  import java.io.IOException;
    24
    25  import javax.management.MalformedObjectNameException;
    26
    27  import org.apache.commons.configuration.Configuration;
    28  import org.neo4j.kernel.AbstractGraphDatabase;
    29  import org.neo4j.server.configuration.Configurator;
    30  import org.rrd4j.ConsolFun;
    31  import org.rrd4j.DsType;
    32  import org.rrd4j.core.RrdDb;
    33  import org.rrd4j.core.RrdDef;
    34
    35  public class RrdFactory
    36  {
    37      public static final int STEP_SIZE = 3000;
    38      public static final int STEPS_PER_ARCHIVE = 750;
    39      private final Configuration config;
    40
    41      public RrdFactory( Configuration config )
    42      {
    43
    44          this.config = config;
    45      }
    46
    47      public RrdDb createRrdDbAndSampler( AbstractGraphDatabase db,
    48                                          JobScheduler scheduler ) throws MalformedObjectNameException, IOException
    49      {
    50          Sampleable[] sampleables = new Sampleable[]{
    51                  new MemoryUsedSampleable(),
    52                  new NodeIdsInUseSampleable( db ),
    53                  new PropertyCountSampleable( db ),
    54                  new RelationshipCountSampleable( db )
    55          };
    56
    57          String basePath = config.getString( Configurator.RRDB_LOCATION_PROPERTY_KEY, getDefaultDirectory( db ) );
    58          RrdDb rrdb = createRrdb( basePath, STEP_SIZE, STEPS_PER_ARCHIVE, sampleables );
    59
    60          RrdSampler sampler = new RrdSampler( rrdb.createSample(), sampleables );
    61          RrdJob job = new RrdJob( sampler );
    62          scheduler.scheduleToRunEveryXSeconds( job, 3 );
    63          return rrdb;
    64      }
    65
    66      private String getDefaultDirectory( AbstractGraphDatabase db )
    67      {
    68          return new File( db.getStoreDir(), "rrd" ).getAbsolutePath();
    69      }
    70
    71      protected RrdDb createRrdb( String inDirectory, int stepSize, int stepsPerArchive,
    72                                  Sampleable... sampleables ) throws IOException
    73      {
    74          if ( !new File( inDirectory ).exists() )
    75          {
    76              RrdDef rrdDef = createRrdDb( inDirectory, stepSize );
    77              defineDataSources( stepSize, rrdDef, sampleables );
    78              addArchives( stepsPerArchive, rrdDef );
    79              return new RrdDb( rrdDef );
    80          } else
    81          {
    82              return new RrdDb( inDirectory );
    83          }
    84      }
    85
    86      private static void addArchives( int stepsPerArchive, RrdDef rrdDef )
    87      {
    88          // Last 35 minutes
    89          rrdDef.addArchive( ConsolFun.AVERAGE, 0.5, 1, stepsPerArchive );
    90
    91          // Last 6 hours
    92          rrdDef.addArchive( ConsolFun.AVERAGE, 0.2, 10, stepsPerArchive );
    93
    94          // Last day
    95          rrdDef.addArchive( ConsolFun.AVERAGE, 0.2, 50, stepsPerArchive );
    96
    97          // Last week
    98          rrdDef.addArchive( ConsolFun.AVERAGE, 0.2, 300, stepsPerArchive );
    99
    100         // Last month
    101         rrdDef.addArchive( ConsolFun.AVERAGE, 0.2, 1300, stepsPerArchive );
    102
    103         // Last five years
    104         rrdDef.addArchive( ConsolFun.AVERAGE, 0.2, 15000, stepsPerArchive * 5 );
    105     }
    106
    107     private static void defineDataSources( int stepSize, RrdDef rrdDef, Sampleable[] sampleables )
    108     {
    109         for ( Sampleable sampleable : sampleables )
    110         {
    111             rrdDef.addDatasource( sampleable.getName(), DsType.GAUGE, stepSize, 0, Long.MAX_VALUE );
    112
    113         }
    114     }
    115
    116     private static RrdDef createRrdDb( String inDirectory, int stepSize )
    117     {
    118         RrdDef rrdDef = new RrdDef( inDirectory, stepSize );
    119         //rrdDef.setVersion( 2 );
    120         return rrdDef;
    121     }
    122 }