aerospike / php-client

Aerospike Client for PHP 8
https://aerospike.github.io/php-client/
9 stars 2 forks source link

Aerospike php8 client return null if data inserted through old client #39

Open piyush-bansal1989 opened 4 months ago

piyush-bansal1989 commented 4 months ago

Hi Team

I'm encountering a data discrepancy with a new client. Data saved through the old client isn't being retrieved correctly by the new client. Specifically, the new client returns a null value for the key (Primary Key), where the old client successfully stores a value.

I also found some data save format discrepancy in old have found format like below. In new client data saving as json string. format through old client 4F 3A 32 39 3A 22 43 6F 6D 70 6F 6E 65 6E 74 73 5C 54 69 6F 5C 54 65 73 74 44 65 74 61 69 6C 73 54 69 6F 22 3A 35 30 3A 7B 73 3A 37 3A 22 61 6D 63 61 74 49 64 22 3B 73 3A 31 34 3A 22 31 31 32 36 30 30 30 35 32 35 33 36 30 35 22 3B 73 3A 36 3A 22 74 65 73

khaf commented 4 months ago

Hi, could you clarify what you mean by the "old client"? An older release of the current PHP 8 compatible version (that we have recently released), or the older C-based version of the client?

piyush-bansal1989 commented 4 months ago

Hi, could you clarify what you mean by the "old client"? An older release of the current PHP 8 compatible version (that we have recently released), or the older C-based version of the client?

The C-based version which supports till php7

khaf commented 4 months ago

Could you please share a code snippet that reproduces the issue (in both the new and old client code)?

Regarding the key value, are you setting the WritePolicy.SendKey to true when writing the key to the database?

piyush-bansal1989 commented 4 months ago

Could you please share a code snippet that reproduces the issue (in both the new and old client code)?

Below are the code snippets for both clients Old php7 client sample code `<?php //use Aerospike; class Testing{ public $a; public $b; public $c; }

$t = new Testing(); $t->a = 4; $t->b = 5;

$config = [];

$config['hosts'][0] =[ 'addr' => "%SERVER_ADDRESS%", 'port' => (int) 3000 ];

$options = [ Aerospike::OPT_POLICY_KEY => Aerospike::POLICY_KEY_SEND, Aerospike::OPT_CONNECT_TIMEOUT => 5000, Aerospike::OPT_READ_TIMEOUT => 5000, Aerospike::OPT_WRITE_TIMEOUT => 5000, Aerospike::OPT_MAX_RETRIES => 3, Aerospike::OPT_SLEEP_BETWEEN_RETRIES => 2000 ]; $aerospike = new Aerospike($config, true,$options); $key = $aerospike->initKey("namespace", "test", "testing"); $content = array( "testing" => $t ); $bins = [ 'content' => $content ]; $response = $aerospike->put($key, $bins, 0, []); if ($response == Aerospike::OK){ echo "data saved in aerospike \n"; } $record = []; $getResponse = $aerospike->get($key, $record, null, []); if ($getResponse == Aerospike::OK){ echo "Get response from Aerospike \n"; print_r($record); }`

New Client php8 sample code `<?php

namespace Aerospike;

$socket = "/var/www/html/asld_grpc.sock"; $client = Client::connect($socket); $key = initKey("namespace", "test", "testing"); $rp = new ReadPolicy(); $rp->setMaxRetries(3); $rp->setSocketTimeout(5000); $record = $client->get($rp, $key);

print_r($record);

function initKey($ns, $set, $key){ return new Key($ns, $set, $key); } `

Regarding the key value, are you setting the WritePolicy.SendKey to true when writing the key to the database?

No. I'll try and let you know

piyush-bansal1989 commented 4 months ago

@khaf : Have you seen this issue?

piyush-bansal1989 commented 4 months ago

Could you please share a code snippet that reproduces the issue (in both the new and old client code)?

Below are the code snippets for both clients Old php7 client sample code `<?php //use Aerospike; class Testing{ public $a; public $b; public $c; }

$t = new Testing(); $t->a = 4; $t->b = 5;

$config = [];

$config['hosts'][0] =[ 'addr' => "%SERVER_ADDRESS%", 'port' => (int) 3000 ];

$options = [ Aerospike::OPT_POLICY_KEY => Aerospike::POLICY_KEY_SEND, Aerospike::OPT_CONNECT_TIMEOUT => 5000, Aerospike::OPT_READ_TIMEOUT => 5000, Aerospike::OPT_WRITE_TIMEOUT => 5000, Aerospike::OPT_MAX_RETRIES => 3, Aerospike::OPT_SLEEP_BETWEEN_RETRIES => 2000 ]; $aerospike = new Aerospike($config, true,$options); $key = $aerospike->initKey("namespace", "test", "testing"); $content = array( "testing" => $t ); $bins = [ 'content' => $content ]; $response = $aerospike->put($key, $bins, 0, []); if ($response == Aerospike::OK){ echo "data saved in aerospike \n"; } $record = []; $getResponse = $aerospike->get($key, $record, null, []); if ($getResponse == Aerospike::OK){ echo "Get response from Aerospike \n"; print_r($record); }`

New Client php8 sample code `<?php

namespace Aerospike;

$socket = "/var/www/html/asld_grpc.sock"; $client = Client::connect($socket); $key = initKey("namespace", "test", "testing"); $rp = new ReadPolicy(); $rp->setMaxRetries(3); $rp->setSocketTimeout(5000); $record = $client->get($rp, $key);

print_r($record);

function initKey($ns, $set, $key){ return new Key($ns, $set, $key); } `

Regarding the key value, are you setting the WritePolicy.SendKey to true when writing the key to the database?

No. I'll try and let you know

@khaf / @vmsachin : can you please provide some update ?

khaf commented 4 months ago

@piyush-bansal1989 Sorry for the late reply. Using the latest PHP client, I cannot reproduce the key issue. The original key value is stored and returned by the client. There is a bug in converting long values in the connection manager that we will address in the release next week. Regarding the object retrieval, the old PHP client would automatically serialize and unserialize PHP objects into blobs. The new client does not do that, so you should do it manually yourself. The byte array you have pasted is incomplete (missing a few bytes at the end), but if it was complete and you passed it to the unserialize function, you would get your object back.

khaf commented 3 months ago

@piyush-bansal1989 Here is the code I used to do it:

$bytes = [0x4F, 0x3A, 0x32, 0x39, 0x3A, 0x22, 0x43, 0x6F, 0x6D, 0x70, 0x6F, 0x6E, 0x65, 0x6E, 0x74, 0x73, 0x5C, 0x54, 0x69, 0x6F, 0x5C, 0x54, 0x65, 0x73, 0x74, 0x44, 0x65, 0x74, 0x61, 0x69, 0x6C, 0x73, 0x54, 0x69, 0x6F, 0x22, 0x3A, 0x35, 0x30, 0x3A, 0x7B, 0x73, 0x3A, 0x37, 0x3A, 0x22, 0x61, 0x6D, 0x63, 0x61, 0x74, 0x49, 0x64, 0x22, 0x3B, 0x73, 0x3A, 0x31, 0x34, 0x3A, 0x22, 0x31, 0x31, 0x32, 0x36, 0x30, 0x30, 0x30, 0x35, 0x32, 0x35, 0x33, 0x36, 0x30, 0x35, 0x22, 0x3B, 0x73, 0x3A, 0x36, 0x3A, 0x22, 0x74, 0x65, 0x73];

$s = pack('C*', ...$bytes);
$object = unserialize($s);

You can of course use serialize to convert the object to a string and then later to a byte array:

$s = serialize($object);