// File: /workspace/github_c_makefile_star2_O2/sharpyfox[P]citrusleaf-client/src/cl_cluster.c
// Function: cluster_tend
static void
cluster_tend(cl_cluster *asc)
{
	pthread_mutex_lock(&asc->LOCK);
	// If thre is already a tending process running for this cluster, there is no
	// point in running one more immediately. Moreover, there are assumptions in
	// the code that only one tender function is running at a time. So, abort.
	if ((asc->state & CLS_FREED) || (asc->state & CLS_TENDER_RUNNING)) {
		cf_debug("Not running cluster tend as the state of the cluster is 0x%x", asc->state);
		pthread_mutex_unlock(&asc->LOCK);
		return;
	}
	asc->state |= CLS_TENDER_RUNNING;

	// Start off by removing dunned hosts
	for (uint i=0;i<cf_vector_size(&asc->node_v);i++) {
		cl_cluster_node *cn = cf_vector_pointer_get(&asc->node_v, i);
		if (!cn)	continue;
		if (cn->dunned) {
			cf_debug(" DELETE DUNNED NODE %s %p i %d",cn->name,cn,i);
			cf_vector_delete(&asc->node_v, i);
			i--;
			cl_partition_table_remove_node(asc, cn);
			cl_cluster_node_release(cn);
		}
	}
	pthread_mutex_unlock(&asc->LOCK);

	// For all registered hosts --- resolve into the cluster's sockaddr_in list
	uint n_hosts = cf_vector_size(&asc->host_str_v);
	cf_vector_define(sockaddr_in_v, sizeof( struct sockaddr_in ), 0);
	for (uint i=0;i<n_hosts;i++) {
		/*
        if (cf_debug_enabled()) {
		    char *host = cf_vector_pointer_get(&asc->host_str_v, i);
		    int port = cf_vector_integer_get(&asc->host_port_v, i);
    		cf_debug("lookup hosts: %s:%d",host,port);
        }
        */
		cl_lookup(asc, cf_vector_pointer_get(&asc->host_str_v, i), 
					cf_vector_integer_get(&asc->host_port_v, i),
					&sockaddr_in_v);
	}
	// Compare this list against the current list of addresses of known nodes.
	// anything new, ping and get its info
	for (uint i=0;i<cf_vector_size(&sockaddr_in_v);i++) {
		struct sockaddr_in *sin = cf_vector_getp(&sockaddr_in_v,i);
		if (0 == cl_cluster_node_get_byaddr(asc, sin)) {
			cluster_ping_address(asc, sin);
		}
	}
	
	if (asc->n_partitions == 0)
		cluster_get_n_partitions(asc, &sockaddr_in_v);
	
	// vector will now contain an accumulation of the service addresses
	cf_vector_reset(&sockaddr_in_v);

	// Now, ping known nodes to see if there's an update
	// No need to lock node list because nodes are only added/removed in this thread.
	for (uint i = 0; i < cf_vector_size(&asc->node_v); i++) {
		cl_cluster_node *cn = cf_vector_pointer_get(&asc->node_v, i);
		cluster_ping_node(asc, cn, &sockaddr_in_v);
		for (uint j=0;j<cf_vector_size(&cn->sockaddr_in_v);j++) {
			struct sockaddr_in *sa_in = cf_vector_getp(&cn->sockaddr_in_v,j);
			cf_vector_append_unique(&sockaddr_in_v, sa_in);
		}
	}
	
	// cf_debug("CLUSTER TEND: sockaddr_in_v len %d %s",cf_vector_size(&sockaddr_in_v), asc->found_all?"foundall":"notfound" );
	
	// Compare all services with known nodes - explore if new
	if (asc->follow == true) {
		int n_new = 0;
		for (uint i=0;i<cf_vector_size(&sockaddr_in_v);i++) {
			struct sockaddr_in *sin = cf_vector_getp(&sockaddr_in_v, i);
			/*
			if (cf_debug_enabled()) {
				dump_sockaddr_in("testing service address",sin);
			}
			*/
			if (0 == cl_cluster_node_get_byaddr(asc, sin)) {
				if (cf_debug_enabled()) {
					cf_print_sockaddr_in("pinging",sin);
				}		
				cluster_ping_address(asc, sin);
				n_new++;
			}
		}
		if (n_new == 0)	{
			//cf_debug("CLUSTER TEND: *** FOUND ALL ***");
			asc->found_all = true;
		}
		//cf_debug("CLUSTER TEND: n_new is %d foundall %d",n_new,asc->found_all);
	}

	cf_vector_destroy(&sockaddr_in_v);

#ifdef DEBUG	
	dump_cluster(asc);
#endif	

	pthread_mutex_lock(&asc->LOCK);
	asc->state &= ~CLS_TENDER_RUNNING;
	pthread_mutex_unlock(&asc->LOCK);

	return;
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0xd48, %rsp            # imm = 0xD48
movq %rdi, %r12
addq $0x158, %rdi            # imm = 0x158
movq %rdi, 0x8(%rsp)
callq 0x23b0
movl 0x10(%r12), %edx
testb $0x3, %dl
je 0x6e80
leaq 0xe482(%rip), %rax      # 0x152c8
movl (%rax), %eax
cmpl $0x3, %eax
jl 0x6e65
leaq 0xe47c(%rip), %rax      # 0x152d0
leaq 0x7c40(%rip), %rsi      # 0xea9b
movq (%rax), %rcx
pushq $0x3
popq %rdi
xorl %eax, %eax
callq *%rcx
movq 0x8(%rsp), %rdi
addq $0xd48, %rsp            # imm = 0xD48
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
jmp 0x2190
orl $0x1, %edx
leaq 0xf8(%r12), %r13
leaq 0x7c46(%rip), %rbx      # 0xead8
movl %edx, 0x10(%r12)
pushq $0x3
popq %rbp
xorl %r14d, %r14d
cmpl 0x104(%r12), %r14d
jae 0x6f11
movq %r13, %rdi
movl %r14d, %esi
callq 0x66c2
testq %rax, %rax
je 0x6f0c
cmpb $0x1, 0x18(%rax)
movq %rax, %r15
jne 0x6f0c
leaq 0xe401(%rip), %rax      # 0x152c8
movl (%rax), %eax
cmpl $0x3, %eax
jl 0x6eeb
leaq 0xe3fb(%rip), %rax      # 0x152d0
movl %ebp, %edi
movq %rbx, %rsi
movq %r15, %rdx
movq %r15, %rcx
movl %r14d, %r8d
movq (%rax), %r9
xorl %eax, %eax
callq *%r9
movq %r13, %rdi
movl %r14d, %esi
callq 0xda54
movq %r12, %rdi
movq %r15, %rsi
decl %r14d
callq 0x81ac
movq %r15, %rdi
callq 0x6cc9
incl %r14d
jmp 0x6e9d
movq %r13, 0x30(%rsp)
movq 0x8(%rsp), %rdi
callq 0x2190
movl 0x24(%r12), %r13d
leaq 0x18(%r12), %rbx
leaq 0x38(%rsp), %r14
pushq $0x10
popq %rsi
leaq 0x540(%rsp), %rdx
movq %r14, %rdi
movl $0x400, %ecx            # imm = 0x400
xorl %r8d, %r8d
xorl %ebp, %ebp
callq 0xd641
leaq 0x60(%r12), %r15
movq %r12, 0x10(%rsp)
cmpl %ebp, %r13d
je 0x6f89
movq %rbx, %rdi
movl %ebp, %esi
callq 0x66c2
movq %r15, %rdi
movl %ebp, %esi
movq %rax, %r12
callq 0x694d
movq 0x10(%rsp), %rdi
movswl %ax, %edx
movq %r12, %rsi
movq %r14, %rcx
callq 0x84a4
incl %ebp
jmp 0x6f56
movq 0x10(%rsp), %r13
xorl %ebx, %ebx
leaq 0x38(%rsp), %r14
cmpl 0x44(%rsp), %ebx
jae 0x6fc7
movq %r14, %rdi
movl %ebx, %esi
callq 0xd9c7
movq %r13, %rdi
movq %rax, %rsi
movq %rax, %r15
callq 0x663c
testq %rax, %rax
jne 0x6fc3
movq %r13, %rdi
movq %r15, %rsi
callq 0x7ef2
incl %ebx
jmp 0x6f95
cmpw $0x0, 0x140(%r13)
jne 0x6fdf
leaq 0x38(%rsp), %rsi
movq %r13, %rdi
callq 0x7d9f
andl $0x0, 0x44(%rsp)
testb $0x2, 0x3c(%rsp)
je 0x7000
movl 0x38(%rsp), %edx
movq 0x48(%rsp), %rdi
xorl %esi, %esi
imull 0x40(%rsp), %edx
callq 0x2140
leaq 0xf8(%rsp), %rbx
leaq 0x38(%rsp), %r15
xorl %esi, %esi
cmpl 0x104(%r13), %esi
jae 0x7308
movq 0x30(%rsp), %rdi
movl %esi, 0x24(%rsp)
callq 0x66c2
movq %rax, %r12
movq %rax, %rbp
xorl %r14d, %r14d
addq $0x20, %r12
cmpl 0x2c(%rbp), %r14d
jae 0x72d9
movq %r12, %rdi
movl %r14d, %esi
callq 0xd9c7
movq %rbp, %rdi
movq %rax, %rsi
leaq 0x80(%rsp), %rdx
callq 0xb0b1
testl %eax, %eax
je 0x709e
leaq 0xe25e(%rip), %rax      # 0x152c8
movl (%rax), %eax
cmpl $0x3, %eax
jl 0x708c
leaq 0xe258(%rip), %rax      # 0x152d0
movq (%rax), %rcx
pushq $0x3
popq %rdi
leaq 0x7a7a(%rip), %rsi      # 0xeaff
movq %rbp, %rdx
xorl %eax, %eax
callq *%rcx
movq %rbp, %rdi
movl $0x12c, %esi            # imm = 0x12C
callq 0x7b79
incl %r14d
jmp 0x7037
cmpb $0x1, 0xac(%rsp)
jne 0x70c7
movq %rbp, %rdi
movl $0x12c, %esi            # imm = 0x12C
callq 0x7b79
leaq 0x80(%rsp), %rdi
callq 0xb2ae
jmp 0x72d9
cmpb $0x0, 0x18(%rbp)
jne 0x70d4
movl $0x0, 0x14(%rbp)
leaq 0x88(%rsp), %rdi
movq %rbp, %rsi
callq 0x2350
testl %eax, %eax
je 0x7127
leaq 0xe1d9(%rip), %rax      # 0x152c8
movl (%rax), %eax
cmpl $0x2, %eax
jl 0x711a
leaq 0xe1d3(%rip), %rax      # 0x152d0
movq (%rax), %r8
pushq $0x2
popq %rdi
leaq 0x7a10(%rip), %rsi      # 0xeb1a
movq %rbp, %rdx
leaq 0x88(%rsp), %rcx
xorl %eax, %eax
callq *%r8
movq %rbp, %rdi
movl $0x321, %esi            # imm = 0x321
callq 0x7b79
movl 0x68(%rbp), %eax
movl 0xa8(%rsp), %ecx
cmpl %ecx, %eax
movl %eax, 0x20(%rsp)
je 0x713c
movl %ecx, 0x68(%rbp)
movq 0xa0(%rsp), %rax
xorl %r14d, %r14d
leaq 0xb0(%rsp), %r13
movl %ecx, 0x1c(%rsp)
movq %r13, %rdi
movq %rax, 0x28(%rsp)
pushq $0x8
popq %rsi
leaq 0x140(%rsp), %rdx
movl $0x400, %ecx            # imm = 0x400
xorl %r8d, %r8d
callq 0xd641
pushq $0x3b
popq %rdi
movq 0x28(%rsp), %rsi
movq %r13, %rdx
callq 0x7b1c
cmpl 0xbc(%rsp), %r14d
jae 0x721a
leaq 0xb0(%rsp), %rdi
movl %r14d, %esi
callq 0x66c2
movq %rax, %r13
movq %rbx, %rdi
pushq $0x8
popq %rsi
leaq 0x940(%rsp), %rdx
movl $0x400, %ecx            # imm = 0x400
xorl %r8d, %r8d
callq 0xd641
pushq $0x3a
popq %rdi
movq %r13, %rsi
movq %rbx, %rdx
callq 0x7b1c
cmpl $0x2, 0x104(%rsp)
jne 0x720a
movq %rbx, %rdi
xorl %esi, %esi
callq 0x66c2
movq %rax, %r13
movq %rbx, %rdi
pushq $0x1
popq %rsi
callq 0x66c2
movq %rax, %rdi
callq 0x2440
movq 0x10(%rsp), %rdi
movswl %ax, %edx
movq %r13, %rsi
movq %r15, %rcx
callq 0x84a4
movq %rbx, %rdi
callq 0xd724
incl %r14d
jmp 0x7183
leaq 0xb0(%rsp), %rdi
callq 0xd724
leaq 0x80(%rsp), %rdi
callq 0xb2ae
movl 0x1c(%rsp), %eax
movq 0x10(%rsp), %r13
cmpl %eax, 0x20(%rsp)
je 0x72d9
movq 0x8(%rsp), %r14
movq %r14, %rdi
callq 0x23b0
movq %r13, %rdi
movq %rbp, %rsi
callq 0x81ac
movq %r14, %rdi
callq 0x2190
xorl %r14d, %r14d
cmpl 0x2c(%rbp), %r14d
jae 0x72d9
movq %r12, %rdi
movl %r14d, %esi
callq 0xd9c7
movq %rbp, %rdi
movq %rax, %rsi
leaq 0x140(%rsp), %rdx
callq 0xb2b6
testl %eax, %eax
je 0x7297
incl %r14d
jmp 0x726a
movq 0x148(%rsp), %rdx
testq %rdx, %rdx
je 0x72b2
movq %r13, %rdi
movq %rbp, %rsi
pushq $0x1
popq %rcx
callq 0x7f72
movq 0x150(%rsp), %rdx
testq %rdx, %rdx
je 0x72cc
movq %r13, %rdi
movq %rbp, %rsi
xorl %ecx, %ecx
callq 0x7f72
leaq 0x140(%rsp), %rdi
callq 0xb491
xorl %r14d, %r14d
cmpl 0x2c(%rbp), %r14d
jae 0x72fd
movq %r12, %rdi
movl %r14d, %esi
callq 0xd9c7
movq %r15, %rdi
movq %rax, %rsi
callq 0xd8f6
incl %r14d
jmp 0x72dc
movl 0x24(%rsp), %esi
incl %esi
jmp 0x700f
cmpb $0x1, 0x14(%r13)
jne 0x7378
xorl %r12d, %r12d
leaq 0x38(%rsp), %rbx
leaq 0x77d9(%rip), %r14      # 0xeaf7
xorl %ebp, %ebp
cmpl 0x44(%rsp), %ebp
jae 0x736e
movq %rbx, %rdi
movl %ebp, %esi
callq 0xd9c7
movq %r13, %rdi
movq %rax, %rsi
movq %rax, %r15
callq 0x663c
testq %rax, %rax
jne 0x736a
leaq 0xdf7e(%rip), %rax      # 0x152c8
movl (%rax), %eax
cmpl $0x3, %eax
jl 0x735c
movq %r14, %rdi
movq %r15, %rsi
callq 0xd3e4
movq %r13, %rdi
movq %r15, %rsi
callq 0x7ef2
incl %r12d
incl %ebp
jmp 0x7320
testl %r12d, %r12d
jne 0x7378
movb $0x1, 0x16(%r13)
leaq 0x38(%rsp), %rdi
callq 0xd724
movq 0x8(%rsp), %rbx
movq %rbx, %rdi
callq 0x23b0
andb $-0x2, 0x10(%r13)
movq %rbx, %rdi
callq 0x2190
addq $0xd48, %rsp            # imm = 0xD48
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq