Skip to content

Commit

Permalink
CACHE: Fix issue #59. Wrong format expected in load_cache method
Browse files Browse the repository at this point in the history
  • Loading branch information
icot committed Sep 28, 2016
1 parent abf2e7f commit 0599118
Show file tree
Hide file tree
Showing 6 changed files with 67 additions and 69 deletions.
4 changes: 1 addition & 3 deletions lib/DBOD/Job.pm
Original file line number Diff line number Diff line change
Expand Up @@ -75,9 +75,7 @@ sub BUILD {
$self->config(DBOD::Config::load()); #load from default share dir

# Load cache file
my %cache = load_cache($self->config->{'api'}->{'cachefile'});
$self->md_cache(\%cache);

$self->md_cache(load_cache($self->config->{'api'}->{'cachefile'}));
# Load entity metadata
$self->metadata(
get_entity_metadata($self->entity, $self->md_cache, $self->config));
Expand Down
11 changes: 7 additions & 4 deletions lib/DBOD/Network/Api.pm
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,6 @@ use DBOD;

our ($VERSION, @EXPORT_OK);

$VERSION = 0.67;
use base qw(Exporter);
@EXPORT_OK = qw( load_cache get_entity_metadata );

Expand All @@ -35,11 +34,15 @@ sub load_cache {
return ();
};

# The metadata cache is stored as an array of hashes
my $json_text = <$json_fh>;
close($json_fh);
my $nested_array = decode_json $json_text;
my @flat_array = map{@$_} @$nested_array;
return @flat_array;
my $array = decode_json $json_text;
my %md_cache;
for my $instance (@{$array}) {
$md_cache{$instance->{db_name}} = $instance;
}
return \%md_cache;
}


Expand Down
96 changes: 47 additions & 49 deletions share/influxdb_entity_example.json
Original file line number Diff line number Diff line change
@@ -1,51 +1,49 @@
[
[
"my_influx",
{
"basedir": "/usr/local/influxdb/influxdb-0.13.0",
"waldir": "/ORA/dbs02/INFLUX_T",
"datadir": "/ORA/dbs03/INFLUX_T/data",
"metadir": "/ORA/dbs03/INFLUX_T/meta",
"configfile":"/ORA/dbs03/INFLUX_T/influxdb.conf",
"pidfile": "/var/run/influxdb-my_influx/influxd.pid",
"environment_file": "/etc/default/influxdb",
"version": "0.13.0",
"query-timeout": "30m",
"max-concurrent-queries": 0,
"log-queries-after": "30m",
"cache-max-memory-size": 524288000,
"cache-snapshot-memory-size": 26214400,
"volumes": [
{
"group": "influxdb",
"file_mode": "0755",
"server": "nas1",
"mount_options": "rw,bg,hard,nointr,tcp,vers=3,noatime,timeo=600,rsize=65536,wsize=65536",
"owner": "influxdb",
"mounting_path": "/ORA/dbs02/INFLUX_T"
},
{
"group": "influxdb",
"file_mode": "0755",
"server": "nas2",
"mount_options": "rw,bg,hard,nointr,tcp,vers=3,noatime,timeo=600,rsize=65536,wsize=65536",
"owner": "influxdb",
"mounting_path": "/ORA/dbs03/INFLUX_T"
}
],
"hosts": [
"localhost"
],
"udp": [
{
"bind-address": 8099,
"database": "gitlab"
}
],
"type": "DEV",
"port": "8086",
"bindir": "/usr/bin",
"subcategory": "InfluxDB"
}
]
{
"db_name": "my_influx",
"basedir": "/usr/local/influxdb/influxdb-0.13.0",
"waldir": "/ORA/dbs02/INFLUX_T",
"datadir": "/ORA/dbs03/INFLUX_T/data",
"metadir": "/ORA/dbs03/INFLUX_T/meta",
"configfile":"/ORA/dbs03/INFLUX_T/influxdb.conf",
"pidfile": "/var/run/influxdb-my_influx/influxd.pid",
"environment_file": "/etc/default/influxdb",
"version": "0.13.0",
"query-timeout": "30m",
"max-concurrent-queries": 0,
"log-queries-after": "30m",
"cache-max-memory-size": 524288000,
"cache-snapshot-memory-size": 26214400,
"volumes": [
{
"group": "influxdb",
"file_mode": "0755",
"server": "nas1",
"mount_options": "rw,bg,hard,nointr,tcp,vers=3,noatime,timeo=600,rsize=65536,wsize=65536",
"owner": "influxdb",
"mounting_path": "/ORA/dbs02/INFLUX_T"
},
{
"group": "influxdb",
"file_mode": "0755",
"server": "nas2",
"mount_options": "rw,bg,hard,nointr,tcp,vers=3,noatime,timeo=600,rsize=65536,wsize=65536",
"owner": "influxdb",
"mounting_path": "/ORA/dbs03/INFLUX_T"
}
],
"hosts": [
"localhost"
],
"udp": [
{
"bind-address": 8099,
"database": "gitlab"
}
],
"type": "DEV",
"port": "8086",
"bindir": "/usr/bin",
"subcategory": "InfluxDB"
}
]
6 changes: 3 additions & 3 deletions share/test.json
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
[
["a", {"prop1":"value1", "prop2": "value2"}],
["b", {"prop1":"value1", "prop2": "value2"}],
["c", {"prop1":"value1", "prop2": "value2"}]
{"db_name":"value1", "prop2": "value2"},
{"db_name":"value1", "prop2": "value2"},
{"db_name":"value1", "prop2": "value2"}
]
13 changes: 6 additions & 7 deletions t/api.t
Original file line number Diff line number Diff line change
Expand Up @@ -34,10 +34,9 @@ $config{'common'} = { template_folder => "${share_dir}/templates" };

# DBOD::Api::load_cache
note( "%config is " . Dumper \%config );
my %cache = DBOD::Network::Api::load_cache($config{'api'}{'cachefile'});
note( Dumper \%cache );

isa_ok(\%cache, 'HASH', 'Cache is a HASH');
my $cache = DBOD::Network::Api::load_cache($config{'api'}{'cachefile'});
note( Dumper $cache );
isa_ok($cache, 'HASH', 'Cache is a HASH/HASHREF');

# We need to Mock the _api_client method on the
# DBOD::Api module
Expand All @@ -63,16 +62,16 @@ subtest 'get_entity_metadata' => sub {
$rest_client->mock('responseCode', sub { return "200" } );
$rest_client->mock('responseContent',
sub { return "{\"response\":[{\"metadata\":\"test\"}]}" } );
ok(DBOD::Network::Api::get_entity_metadata('unexistant', \%cache, \%config),
ok(DBOD::Network::Api::get_entity_metadata('unexistant', $cache, \%config),
"Method call");
my $metadata = DBOD::Network::Api::get_entity_metadata('unexistant', \%cache,
my $metadata = DBOD::Network::Api::get_entity_metadata('unexistant', $cache,
\%config);
isa_ok($metadata, 'HASH', 'Result is a HASH/HASHREF');

# Test failure
$rest_client->mock('responseCode', sub { return "404" } );
$rest_client->mock('responseContent', sub { return "{\"response\": []}" } );
$metadata = DBOD::Network::Api::get_entity_metadata('unexistant', \%cache, \%config);
$metadata = DBOD::Network::Api::get_entity_metadata('unexistant', $cache, \%config);
isa_ok($metadata, 'HASH', 'Result is a HASH/HASHREF');
ok(!exists $metadata->{response}, "Result has empty metadata field");
};
Expand Down
6 changes: 3 additions & 3 deletions t/influxdb.t
Original file line number Diff line number Diff line change
Expand Up @@ -23,11 +23,11 @@ BEGIN { Log::Log4perl->easy_init( {

my $filename = DBOD::Config::get_share_dir() . "/influxdb_entity_example.json";

my %cache = DBOD::Network::Api::load_cache($filename);
note( Dumper \%cache );
my $cache = DBOD::Network::Api::load_cache($filename);
note( Dumper $cache );

my $instancename = "my_influx";
my $metadata = $cache{$instancename};
my $metadata = $cache->{$instancename};

my $config = {
influxdb => {
Expand Down

0 comments on commit 0599118

Please sign in to comment.