- 01
- 02
- 03
- 04
- 05
- 06
- 07
- 08
- 09
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
- 23
- 24
- 25
- 26
- 27
- 28
- 29
- 30
- 31
- 32
- 33
- 34
- 35
- 36
- 37
- 38
- 39
- 40
- 41
- 42
- 43
- 44
- 45
- 46
- 47
- 48
- 49
- 50
- 51
- 52
- 53
// На самом деле это пока только псевдокод. Real code will be on C/C++
//Lock-free non blocking and anatomic operation only if IP was changed
// IN SHARED MEMORY
non_atomic_in_shmem bad_IP_flag_non_atomic_in_shmem[64][16]; // in real code it will be uint64_t array[16] and bits operations with it
some_ip_class IPs_non_atomic_in_shmem[64][16] //
std::atomic<uint64_t> version_holder_atomic_in_shmem[64][16]; // it is debatable "whether to do it the Atomic" but for reinsurance
// IN WRITER PROCESS
void worker_reassign_IP_by_num(new_IP, back_end_server_ID, num)
if bad_IP_flag_non_atomic_in_shmem[back_end_server_ID][num] // in real code this check will be not here but will have same sense
version_holder_atomic_in_shmem[back_end_server_ID][num] ++ //it is increment of atomic var version and version become NOT even
__sync_synchronize(); //TODO may be we can use something better here ?
IPs_non_atomic_in_shmem[back_end_server_ID][num] = new_IP //it copy assignment
__sync_synchronize(); //TODO may be we can use something better here ?
version_holder_atomic_in_shmem[back_end_server_ID][num] ++ //it is increment of atomic var version and version become even
__sync_synchronize(); //TODO may be we can use something better here ?
bad_IP_flag_non_atomic_in_shmem[back_end_server_ID][num] = 0
// IN READER PROCESS
some_ip_class get_valid_ip_by_num(back_end_server_ID,num){
//TODO version_holder is atomic for now, may we do it non atomic? I think YES. Are any arguments in opposites?
// Instead of atomic can we possible use volatile?
uint64_t version_before = version_holder_atomic_in_shmem[back_end_server_ID][num] //it is atomic copy assignment
//if (version_befor & 1) // is not even it will work too instead of checking flags
// but checking flags it is less expensive operation than checking version because the version var is atomic
if bad_IP_flag_non_atomic_in_shmem //
return 0;
some_ip_class ip = IPs_non_atomic_in_shmem[back_end_server_ID][num]
uint64_t version_after = version_holder_atomic_in_shmem[back_end_server_ID][num] // it is atomic copy assignment
if version_before != version_after // versions is not sames
// we may add extra check of version evenest but it will cost us 1 slow atomic operation and excessively
//OR (version_after & 1) //or versions is not even ip is not correct
return 0
return ip
}
some_ip_class get_valid_ip(back_end_server_ID)
while(time_not_expaired) {
for (n=0; n<16; n++){
some_ip_class ip = get_valid_ip_by_num(back_end_server_ID, n)
if ip
return ip
}
//"it will never happened" "if it happened and time expired do something but it is problems on the back end servers site"
//”it is similar situation with situation when back end server just down ”
}
return 0 // or some server_down_ip ;)
some_ip_class check_or_get_new_ip( curent_ip, back_end_server_ID, num)
if NOT bad_IP_flag_non_atomic_in_shmem
if current_ip == shmem_array_of_IP_non_atomic[back_end_server_ID][num] // maybe need copy but I think it's not necessary
return current_ip