2

I have the problem that I will have multiple servers that perform jobs. They all request a joblist from the same API and need to identify them with a unique id. So that I know which server is running which jobs and have no distribution collisions etc.

I am now trying to figure out a way to generate a unique server id. It is not critical that it stays the same forever. But it should be unique.

I noticed the PHP build time in phpinfo() but couldn't find any documentation about it. What if php is installed as a binary package? It will probably not change if two servers use the same package.

I would prefer some sort of mother board serial number or something.

A yeah, and it should be portable. So I would rather like to avoid something like calling a shell command using shell_exec or so.

Going for the external IP could also be an option. But how to reliably determine the "external" ip adress? what if there are more? What if its natted? Then i would need a combination of external and internal adress.

I could request the API one time and ask it to give me the external ip adress and combine. But this is going way to complicated and extensive...

Do you have any recommendations on this?

dsolimano
  • 8,870
  • 3
  • 48
  • 63
The Surrican
  • 29,118
  • 24
  • 122
  • 168
  • Is there something here? http://php.net/manual/en/reserved.variables.server.php – Rimian Feb 23 '11 at 11:40
  • possible duplicate of [How to generate unique ids for my scaled Servers with PHP?](http://stackoverflow.com/questions/5088922/how-to-generate-unique-ids-for-my-scaled-servers-with-php) – Mark Baker Feb 23 '11 at 11:49
  • no, its not a duplicate of that @Mark Baker. because i don't need to generate just a uniqu id, i have the requirement that its unique per system and stays the same when applied the same algorithm again. – The Surrican Feb 23 '11 at 12:08
  • You wrote "It is not critical that it stays the same forever" - how does it match the 'stays the same' statement? – Grzegorz Oledzki Feb 23 '11 at 19:13
  • "not forever" does not mean every time. it needs to stay the same for the process to work. but if it changes, some jobs get "lost". just like if a server breaks down. then a cleanup job fetches them. the application will work just fine. but it will not work or only with a huge overhead if it changes too much. – The Surrican Feb 23 '11 at 20:21

3 Answers3

1

I would say give them an Id.

Your server is the master, build a protocol, if the server is not giving you is unique Id in the sent query then build a new record for this new server and give him the record id. Then Your protocol on the joblist query state that the client server should now reuse the given Id. if he does not he will get a new id each time (which is unique).

You'll face the problem, maybe, of a growing number of Id, then add a TTL to each Id, and perform some cleanup (or simply wipe out this table sometimes). In your protocol you should now at a way to tell your client server his given Id has been invalidated and that you give him a new one.

If you really want to delegate this 'unique Id' management to the clients, then ask them to generate a random uid, and prey that it do not collide (very low %, but it can happen, but maybe not before you die). You'll have to store it on your sied, and manage the growing number of Id as well. But there you will simply take the server given Id as a new one after wipeout, you don't need to ask client to rebuild a new Id. Simplier.

regilero
  • 29,806
  • 6
  • 60
  • 99
  • thats what i am doing right now :) i cache them in apc in the application. thanks for the answer and covering more eventualities. it helped me. i just wonder whether there is a nice way to generate something from the system environment... – The Surrican Feb 23 '11 at 12:06
0

uniqid() gives you a unique ID for which chances are pretty rare that IDs collide.

tobyS
  • 860
  • 1
  • 7
  • 15
  • i think you mis understood the question. i need a unique id that identifies the system. the chances of uniqid colliding in the same server a zero. therefore it has a prefix parameter. – The Surrican Feb 23 '11 at 12:04
  • Since `uniqid()` relies on microseconds, chances are also rare that it collides between multiple servers. Generating the unique ID on a server, when it asks for jobs for the first time, should be sufficiently unique. Or am I wrong? You can maybe also use the hostname as a prefix, if that is set properly. – tobyS Feb 23 '11 at 12:08
0

You can use Mac Address as unique id for your servers: https://stackoverflow.com/a/1420402/318765

Community
  • 1
  • 1
Abhishek Saha
  • 93
  • 1
  • 4