📜 ⬆️ ⬇️

Memcached, PHP, Ketama. If not, but really want.

I want to.
Ketama in PHP, and even so that at least from what access was.
This is not about PHP. In terms of access. Well, from PHP too.

Well, let's get started.
What we have.
Inet, Google ...
… everything :(
Ahh, I almost forgot. The most important thing. The ceiling, so it was stupid to stare.

So google.
There is no ketama in PHP. And where is that.
Gee, on last.fm.
www.lastfm.ru/user/RJ/journal/2007/04/10/rz_libketama_-_a_consistent_hashing_algo_for_memcache_clients
Only after April 10, 2007, version 0.1.1 has not been updated by anyone, and the functionality is somewhat scanty. On danga. com already gets'y, cas'y, etc. crap full. Can it be possible, but that's just to figure it out.
')
Google further ...
tangent.org

What we have.
Memcached Functions for MySQL
libmemcached 0.23

WOW !!!!
Clients using the library:
Ruby: github.com/fauna/memcached/tree/master
Perl: code.google.com/p/perl-libmemcached
Python: code.google.com/p/python-libmemcached
PHP: (In Japanese) labs.gree.jp/Top/OpenSource/libmemcached.html

We go there where PHP.
GUARD!!! Shalashiki.
Damn and version 0.1.0. But fresh, from 09/18/2008. In short, glitches in it have not yet been fixed, because no one had time to look for them.
… everything.
There is nothing more.

Well, we will try.
Probably it is better not to start with slut.
Although I have a bourgeois whole vocabulary: hi, yes, no, problem, thank and everything.
... well, not all of course, there is still a fuck, but he will not help me.
In Japanese it is shorter.

I put libmemcached and Memcached Functions for MySQL on the database server. On the web server libmemcached.
Raise four memcached daemons. One on the web server, three on the database server. Why? And FIG knows, it happened.
I install functions.

Hmm, the list is impressive.
memc_add
memc_add_by_key
memc_servers_set
memc_server_count
memc_set
memc_set_by_key
memc_cas
memc_cas_by_key
memc_get
memc_get_by_key
memc_delete
memc_delete_by_key
memc_append
memc_append_by_key
memc_prepend
memc_prepend_by_key
memc_increment
memc_decrement
memc_replace
memc_replace_by_key
memc_servers_behavior_set
memc_udf_version
memc_list_behaviors
memc_stats
memc_stat_get_keys
memc_stat_get_value

Not understood. If there is a cas? Where is gets What the hell I gave up cas without gets'a !!!
Okay, we will remember them. Probably.

We define server
SELECT memc_servers_set('192.168.0.10:11211, 192.168.0.11:11211, 192.168.0.11:11212, 192.168.0.11:11213);

We shove a cache of a dozen keys
SELECT memc_set('key1', 'val1');

SELECT memc_set('key10', 'val10');


We take them from the cache
SELECT memc_get('key1');

SELECT memc_get('key10');


It seems normal, gives what they put.

Now we bring down one memcached daemon. As expected, the two keys are covered. We again put in the cache 10 of the same keys.
FIG you. Those missing two never showed up. Ketama and does not smell. Well, nothing, readme read me not zapadlo.

In Memcached Functions for MySQL, I did not find anything clever. Let's go to tangent.org .
Damn, how everything is running.

Yeah, that's about it.
docs.tangent.org/libmemcached/memcached_behavior.html
According to translate.ru behavior this behavior.
By default MEMCACHED_BEHAVIOR_DISTRIBUTION costs MEMCACHED_DISTRIBUTION_MODULA, and we need MEMCACHED_DISTRIBUTION_CONSISTENT, and MEMCACHED_BEHAVIOR_HASH to be MEMCACHED_HASH_KETAMA.

And memcached_behavior_set () does that. In MySQL, this is supposed to be memc_servers_behavior_set.
Brrr, stop. And what is this in the dock memcached_behavior.pod

= item MEMCACHED_BEHAVIOR_DISTRIBUTION
Using this website you can enable values.
The default method is MEMCACHED_DISTRIBUTION_MODULA. You can enable
consistent hashing by setting MEMCACHED_DISTRIBUTION_CONSISTENT.
Consistent hashing delivers better distribution
added. Currently
MEMCACHED_DISTRIBUTION_CONSISTENT is an alias for the value
MEMCACHED_DISTRIBUTION_CONSISTENT_KETAMA.

= item MEMCACHED_BEHAVIOR_KETAMA
Sets the default distribution to MEMCACHED_DISTRIBUTION_CONSISTENT_KETAMA
and the hash to MEMCACHED_HASH_MD5.

Something porridge in my head begins.
Yeah, I probably understand, you need to do MEMCACHED_BEHAVIOR_KETAMA ...
Like this?
Let's go on reading. It seems stupidly need everything. More on bourgeois understand byyyy.

It looks like it
If you want to make a difference, it’s worth
So you need MEMCACHED_BEHAVIOR_KETAMA set 1.

Well, let's try.
SELECT memc_servers_behavior_set ('MEMCACHED_BEHAVIOR_KETAMA', 1);

Mdaaa. Kirdyk full.
mysql>SELECT memc_servers_behavior_set('MEMCACHED_BEHAVIOR_KETAMA', 1);
ERROR 2013 (HY000): Lost connection to MySQL server during query
mysql>

It is treated only restart MySQL server ...

Okay. We will write to developers
forums.mysql.com/read.php?150 , 231325,231325 # msg-231325

Quick ... unsubscribed
forums.mysql.com/read.php?150 , 231325,231359 # msg-231359

And if so
forums.mysql.com/read.php?150 , 231325,231442 # msg-231442
That's the same thing.

It looks like a long time.
We'll have to look blankly at the ceiling. Maybe there is something?
For sure. There is. Only here some thoughts are not clever, but dirty.
If from where something is taken is not what I need. So somewhere it is put there. You just need to find it somewhere and put there what I need.

Oh damn. Climb to the source. And they are on it. And I only know the name of C. In the sense that the language is so called.
Anyway. There is nothing to do, we will look for familiar words.
In MySQL, the library is probably not worth it, it is so, laying. We climb into libmemcached itself.

Yeah, that's it. In memcached.c. line 29
ptr-> distribution = MEMCACHED_DISTRIBUTION_MODULA;
whole two familiar words.

And the construction of the name is promising: memcached_st * memcached_create (memcached_st * ptr).
Now we will try to understand who memcached_st is.
Probably understandable. According to libmemcached.pod, if translate.ru correctly explained everything to me, this is the very place where it is not what I need.
Let's go again on tangent.org . Konfy is not there, but there is mailing worm lists.tangent.org/mailman/listinfo/libmemcached . There is not much of that. Stupidly look for familiar words.

There is something.
lists.tangent.org/pipermail/libmemcached/2008-September/000434.html
If you understand correctly, you need MEMCACHED_BEHAVIOR_SERVER_FAILURE_LIMIT to do some set. They write 10, I will try 3.

lists.tangent.org/pipermail/libmemcached/2008-August/000429.html
Well, we'll do the same.

So. Try to correct the source. In the sense of putting what we need.

Instead of ptr-> distribution = MEMCACHED_DISTRIBUTION_MODULA;
Write ptr-> distribution = MEMCACHED_DISTRIBUTION_CONSISTENT_KETAMA;
Wow. My first line in life is 0.5 C.

Instead of ptr-> retry_timeout = 0;
Write ptr-> retry_timeout = 60;

Yyyyyyyyy. Instead write about MEMCACHED_BEHAVIOR_SERVER_FAILURE_LIMIT?
Again, we stupidly look at the ceiling.

And what to watch that. Search for SERVER_FAILURE_LIMIT in source code.
There is. In memcached_behavior.c. Page 166 is called ptr-> server_failure_limit.
Add by analogy with the rest
ptr-> server_failure_limit = 3;
Maybe a ride. In principle, if anything - the server will fall cancer. So also put it.

That's what happened

memcached_st *memcached_create(memcached_st *ptr)
{
memcached_result_st *result_ptr;

if (ptr == NULL)
{
ptr= (memcached_st *)malloc(sizeof(memcached_st));

if (!ptr)
return NULL; /* MEMCACHED_MEMORY_ALLOCATION_FAILURE */

memset(ptr, 0, sizeof(memcached_st));
ptr->is_allocated= MEMCACHED_ALLOCATED;
}
else
{
memset(ptr, 0, sizeof(memcached_st));
}
result_ptr= memcached_result_create(ptr, &ptr->result);
WATCHPOINT_ASSERT(result_ptr);
ptr->poll_timeout= MEMCACHED_DEFAULT_TIMEOUT;
ptr->connect_timeout= MEMCACHED_DEFAULT_TIMEOUT;
ptr->retry_timeout= 60;
ptr->distribution= MEMCACHED_DISTRIBUTION_CONSISTENT_KETAMA;
ptr->server_failure_limit= 3;

return ptr;
}

Everything. Recompile and forward. We start onany ... well, you understand, to iterations.

We define server
SELECT memc_servers_set('192.168.0.10:11211, 192.168.0.11:11211, 192.168.0.11:11212, 192.168.0.11:11213);

We shove a cache of a dozen keys
SELECT memc_set('key1', 'val1');

SELECT memc_set('key10', 'val10');


We take them from the cache
SELECT memc_get('key1');

SELECT memc_get('key10');


It seems normal, gives what they put.

We bring down one daemon memcached.
Not understood. What the heck. Why are they ALL alive? This is about the data that I put in the cache.

Okay. Valim second.
Well, we did not agree. They are all alive.

Well, if the third.
Damn, all alive.

Well, we are so easy not to take. There is no reception against scrap. Valim of the last fourth daemon memcached.
That is the same. Live left.
We begin to kill, lift, save in, get out of ...
... shorter iterations to do.
I CAN'T KILL !!!

Hmm. Works. Even more than that.

Well no. We are not so easy to take.
Everything is KILLED !!!
Left alive one node. In the quiet raised the second. Not arranging any calls to memcached, I dropped the first one.
All did not survive. Know our who you want cancer put.

Uff. What am I talking about?
Actually about PHP.

Well, let's go to the hut labs.gree.jp/Top/OpenSource/libmemcached.html

So, thank God, the source code is not in a hut.
github.com/kajidai/php-libmemcached/tree/master

We screw on.
labs.gree.jp/Top/OpenSource/libmemcached/Document.html

We try

<?php
$memcached = new Memcached();
$memcached->addserver('192.168.0.10', 11211);
$memcached->addserver('192.168.0.11', 11211);
$memcached->addserver('192.168.0.11', 11212);
$memcached->addserver('192.168.0.11', 11213);
$memcached->set('key1', 'val1');
$ret = $memcached->get('key1');
Echo $ret. “<br>”;


It works bastard ...
Together with MySQL works.
Correctly the data work in a pair.
Valim, we lift, we write, we read - everything is correct.

By the way. On the webserver, a file of libmemcached is screwed.

And what about the functionality we have here?

Not bad
memcached_ctor
memcached_server_add
memcached_add
memcached_add_by_key
memcached_append
memcached_append_by_key
memcached_behavior_get
memcached_behavior_set
memcached_cas
memcached_cas_by_key
memcached_delete
memcached_delete_by_key
memcached_get
memcached_get_by_key
memcached_set
memcached_set_by_key
memcached_increment
memcached_decrement
memcached_prepend
memcached_prepend_by_key
memcached_replace
memcached_replace_by_key
memcached_server_list
memcached_mget
memcached_fetch
memcached_server_list_append
memcached_server_push

gets'a no either.

And what about the glitches ...
And FIG knows.
To find out. Well, in fact, do not write the same author in my Russian English to Japan. After all, he will answer me in his Japanese English.
So the war will begin.

Okay, let's get into the source. After all, I have already written 2.5 lines in total in my life.

Damn, it's all just ...
Ahhh, drove in. He simply wrote a call wrapper for broadcast calls. Damn and nakosyachit then probably there is nowhere special.

Well.
Let's sum up.

First of all. I wrote my first 2.5 lines in C.
Secondly. I now have ketama in PHP.
Thirdly. Fuck this cache so easy to fall down.
Fourth. Fucking from what, normal access to the data in memcached, even from C, at least from Perl, at least from PHP with MySQL. One lays, do not care where and who the other reads correctly.

Ps. Hats off to Brian Aker brian.krow.net

Source: https://habr.com/ru/post/43156/


All Articles