Difference between revisions of "Accounting Library"

From Gcube Wiki
Jump to: navigation, search
(Usage Record Record Harmonization)
(Development)
 
(40 intermediate revisions by 3 users not shown)
Line 1: Line 1:
This component is responsible of collecting, harmonizing and storing accounting data.
+
{| align="right"
 +
||__TOC__
 +
|}
  
== Accounting Lib in the Accounting Architecture ==
+
[[Category: Developer's Guide]]
  
The following image show the components discussed in this wiki page in the full [[Accounting | Accounting Architecture]]:
+
This component is responsible of collecting, harmonizing and storing '''Accounting''' data.
 +
 
 +
== Overview ==
 +
 
 +
The following image evidences the accounting-lib component as part of [[Accounting | Accounting Architecture]]:
  
 
[[File:Accounting-architecture-accounting-lib.png]]
 
[[File:Accounting-architecture-accounting-lib.png]]
  
 
== Library ==
 
== Library ==
The library has been tough to provide a common way to elaborate '''Usage Record''' (''UR'') (see [[Accounting Model]] to check which Usage Records are supported by default). In particular is responsible for:
+
The library has been implemented to provide a common way to elaborate '''Usage Record''' (''UR'') (see [[Accounting Model]] to check which Usage Records are supported by default). In particular is responsible for:
* Collecting
+
* Collecting Accounting Data
 +
 
 +
It uses facilities provided by [[Document Store Library | Document Store Library]] for:
 +
 
 
* Aggregating
 
* Aggregating
 
* Buffering
 
* Buffering
 
* Storing
 
* Storing
 +
* Providing ''Fallback and Retry'' capability
  
 
=== Collecting Usage Record ===
 
=== Collecting Usage Record ===
  
The library allow to collect Usage Records providing the following functionality:
+
The library allows to collect Usage Records providing the following functionality:
  
 
* Attribute Validation and Harmonization
 
* Attribute Validation and Harmonization
Line 25: Line 35:
 
* Generic Key-Value method (key:String, value:Comparable<? extends Serializable> )
 
* Generic Key-Value method (key:String, value:Comparable<? extends Serializable> )
  
For the same attribute each of the record is idempotent. This means that any of the way you are using to set a properties the value is always validated and harmonized by the same code.  
+
The methods are idempotent. This means that any of the way you are using to set a properties the value is always validated and harmonized in the same way (i.e. from the the same code).  
Different methods for different properties can be used together.
+
You can mix different methods for different properties.
  
 
Moreover you can provide a Map<String,Comparable<? extends Serializable>> to set all properties at once.
 
Moreover you can provide a Map<String,Comparable<? extends Serializable>> to set all properties at once.
  
==== Usage Record Record Harmonization and Validation ====
+
==== Usage Record Validation and Harmonization ====
  
This library provide support to harmonize the values to a common type (e.g. the string representation on an integer to a integer type).
+
This library provides support to validate and harmonize the values to a common type (e.g. the string representation on an integer to a integer type, the string representation of an enum to the enum type).
 
This functionality is very useful especially when the developer use a the key-value method.
 
This functionality is very useful especially when the developer use a the key-value method.
  
==== Usage Record Record Validation ====
+
For further details see [[Accounting Model]].
  
 +
== Code Example ==
  
==== Internal Representation ====
+
The following snippet of code provide an example of '''ServiceUsageRecord''' account which is automatically made from wHN and gHN.
 +
See [[Accounting Model#ServiceUsageRecord | Service Usage Record Model]]
  
  
==== Internal Facilities ====
 
  
=== Buffering & Aggregating Usage Record ===
+
<syntaxhighlight lang="java">
  
The library provide bufferization capabilities.
+
// This code auto-discover the available persistence and retrieve the configuration from IS to correctly initialize it.
While the records are buffered for each new records requested to be accounted the library try to aggregate the new record with the one has already buffered. The aggregation made in this phase is lossless.
+
AccountingPersistence accountingPersistence = AccountingPersistenceFactory.getPersistence();
  
=== Storing Usage Record ===
+
// Create a new Service Usage Record
 +
ServiceUsageRecord usageRecord = new ServiceUsageRecord();
  
* Fallback
+
/*  
 +
* Commented to show the use of key-value setResourceProperty function
 +
* instead of the convenient setter
 +
* usageRecord.setConsumerId("luca.frosini");
 +
* Please note that as KEY is used the provided static variable. Always behave like this.
 +
*/
 +
usageRecord.setResourceProperty(ServiceUsageRecord.CONSUMER_ID, "luca.frosini");
  
== Development ==
+
usageRecord.setOperationResult(OperationResult.SUCCESS);
 +
usageRecord.setCallerHost("node13.d4science.org");
 +
usageRecord.setHost("node22.d4science.org");
 +
usageRecord.setServiceClass("VREManagement");
 +
usageRecord.setServiceName("SmartExecutor");
 +
usageRecord.setCalledMethod("launch");
  
The library is identified by the following maven coordinates:
 
  
''<groupId>'''''org.gcube.accounting'''''</groupId>''
+
// usageRecord.setDuration(300)
 +
// Duration should be an integer. Anyway the string is converted from the validator.
 +
usageRecord.setResourceProperty(ServiceUsageRecord.DURATION, "300");
  
''<artifactId>'''''accounting-lib'''''</artifactId>''
+
accountingPersistence.account(usageRecord);
  
 +
</syntaxhighlight>
  
== Persistence ==
 
  
The current implementation of persistence for this library is for CouchDB storage.
+
Please note that ServiceUsage record is accounted automatically from smartgears. Smartgears auto calculate '''calledMethod''' by using the relative path of the URL.
 +
This led to have records which cannot be aggregated from [[Accounting Aggregator]]. Please refer to the section [[Accounting_Aggregator#How_to_manage_calledMethod_cannot_be_aggregated | How to manage calledMethod lead to ServiceUsageRecord cannot be aggregated ]] to solve such an issue.
 +
 
 +
== Development ==
 +
 
 +
The library is identified by the following maven coordinates:
  
 
''<groupId>'''''org.gcube.accounting'''''</groupId>''
 
''<groupId>'''''org.gcube.accounting'''''</groupId>''
  
''<artifactId>'''''accounting-lib-persistence-couchdb'''''</artifactId>''
+
''<artifactId>'''''accounting-lib'''''</artifactId>''
 +
 
 +
Every components has to account ''UsageRecord''s must depend from this library.
 +
 
  
   
+
In some cases (e.g. Exception management) you may also depended from [[Document Store Library | Document Store Library]]. The maven coordinates of  Document Store  Lib are:
=== Less dependency is better than more ===
+
  
This library must be available on every node of the infrastructure. To respect as much possible ZERO-DEPENDENCY paradigm of SmartGears a simple couchdb-connector (simple HTTP client library) has been developed.
+
''<groupId>'''''org.gcube.data-publishing'''''</groupId>''
  
 +
''<artifactId>'''''document-store-lib'''''</artifactId>''
  
  
== Deployment ==
+
Please set the scope of both library to '''PROVIDED''' if your component runs on a container gHN or vHN.

Latest revision as of 16:12, 6 December 2019

This component is responsible of collecting, harmonizing and storing Accounting data.

Overview

The following image evidences the accounting-lib component as part of Accounting Architecture:

Accounting-architecture-accounting-lib.png

Library

The library has been implemented to provide a common way to elaborate Usage Record (UR) (see Accounting Model to check which Usage Records are supported by default). In particular is responsible for:

  • Collecting Accounting Data

It uses facilities provided by Document Store Library for:

  • Aggregating
  • Buffering
  • Storing
  • Providing Fallback and Retry capability

Collecting Usage Record

The library allows to collect Usage Records providing the following functionality:

  • Attribute Validation and Harmonization
  • Usage Record Validation

To set the various properties of an usage records two different methods are available

  • Developer Friendly methods (getter and setter)
  • Generic Key-Value method (key:String, value:Comparable<? extends Serializable> )

The methods are idempotent. This means that any of the way you are using to set a properties the value is always validated and harmonized in the same way (i.e. from the the same code). You can mix different methods for different properties.

Moreover you can provide a Map<String,Comparable<? extends Serializable>> to set all properties at once.

Usage Record Validation and Harmonization

This library provides support to validate and harmonize the values to a common type (e.g. the string representation on an integer to a integer type, the string representation of an enum to the enum type). This functionality is very useful especially when the developer use a the key-value method.

For further details see Accounting Model.

Code Example

The following snippet of code provide an example of ServiceUsageRecord account which is automatically made from wHN and gHN. See Service Usage Record Model


// This code auto-discover the available persistence and retrieve the configuration from IS to correctly initialize it.
AccountingPersistence accountingPersistence = AccountingPersistenceFactory.getPersistence();
 
// Create a new Service Usage Record
ServiceUsageRecord usageRecord = new ServiceUsageRecord();
 
/* 
 * Commented to show the use of key-value setResourceProperty function 
 * instead of the convenient setter
 * usageRecord.setConsumerId("luca.frosini");
 * Please note that as KEY is used the provided static variable. Always behave like this.
 */
usageRecord.setResourceProperty(ServiceUsageRecord.CONSUMER_ID, "luca.frosini");
 
usageRecord.setOperationResult(OperationResult.SUCCESS);
usageRecord.setCallerHost("node13.d4science.org");
usageRecord.setHost("node22.d4science.org");
usageRecord.setServiceClass("VREManagement");
usageRecord.setServiceName("SmartExecutor");
usageRecord.setCalledMethod("launch");
 
 
// usageRecord.setDuration(300)
// Duration should be an integer. Anyway the string is converted from the validator.
usageRecord.setResourceProperty(ServiceUsageRecord.DURATION, "300");
 
accountingPersistence.account(usageRecord);


Please note that ServiceUsage record is accounted automatically from smartgears. Smartgears auto calculate calledMethod by using the relative path of the URL. This led to have records which cannot be aggregated from Accounting Aggregator. Please refer to the section How to manage calledMethod lead to ServiceUsageRecord cannot be aggregated to solve such an issue.

Development

The library is identified by the following maven coordinates:

<groupId>org.gcube.accounting</groupId>

<artifactId>accounting-lib</artifactId>

Every components has to account UsageRecords must depend from this library.


In some cases (e.g. Exception management) you may also depended from Document Store Library. The maven coordinates of Document Store Lib are:

<groupId>org.gcube.data-publishing</groupId>

<artifactId>document-store-lib</artifactId>


Please set the scope of both library to PROVIDED if your component runs on a container gHN or vHN.