Repository: microsoft/garnet
File: libs/cluster/Session/MigrateCommand.cs (C#)

Code before:
            var transferOption = TransferOption.NONE;
            string effectiveAddress = targetAddress;
            if (clusterProvider.serverOptions.EnableCluster)
            {
                pstate = MigrateCmdParseState.SUCCESS;
                current = clusterProvider.clusterManager.CurrentConfig;
                sourceNodeId = current.LocalNodeId;

                // First try to find worker by the provided address (could be IP or hostname)
                targetNodeId = current.GetWorkerNodeIdFromAddressOrHostname(targetAddress, targetPort);

                // If not found directly, check if targetAddress is a hostname and try to resolve it
                if (targetNodeId == null && !IPAddress.TryParse(targetAddress, out _))
                {
                    // targetAddress is not a valid IP, try to resolve it as hostname
                    try
                    {
                        var hostEntry = Dns.GetHostEntry(targetAddress);
                        if (hostEntry.AddressList.Length > 0)
                        {
                            // Prefer IPv4 addresses to match most common cluster configurations
                            var resolvedIp = hostEntry.AddressList.FirstOrDefault(a => a.AddressFamily == System.Net.Sockets.AddressFamily.InterNetwork)
                                          ?? hostEntry.AddressList[0]; // Fallback to first address if no IPv4 found
                            effectiveAddress = resolvedIp.ToString();
                            // Try again with the resolved IP address
                            targetNodeId = current.GetWorkerNodeIdFromAddressOrHostname(effectiveAddress, targetPort);
                        }
                    }
                    catch (System.Net.Sockets.SocketException ex)
                    {
                        // Hostname resolution failed (DNS failure, network issue, etc.)
                        logger?.LogWarning(ex, "Failed to resolve hostname {hostname}", targetAddress);
                        pstate = MigrateCmdParseState.HOSTNAME_RESOLUTION_FAILED;
                    }
                }

                if (targetNodeId == null && pstate == MigrateCmdParseState.SUCCESS)
                    pstate = MigrateCmdParseState.UNKNOWNTARGET;
            }

            // Add single key if specified
            if (keySlice.Length > 0)
            {
                transferOption = TransferOption.KEYS;
                sketch = new();
                sketch.HashAndStore(ref keySlice);
            }

            var currTokenIdx = 5;
            while (currTokenIdx < parseState.Count)
            {

Review comment (suggestion):
@copilot, if we resolve multiple ip addresses we need to find the one that is matched against the cluster config. So, you need to test each one and get the first one that matches instead of defaulting on the first one in the list

Code after:
            string effectiveAddress = targetAddress;
            if (clusterProvider.serverOptions.EnableCluster)
            {
                pstate = MigrateCmdParseState.SUCCESS;
                current = clusterProvider.clusterManager.CurrentConfig;
                sourceNodeId = current.LocalNodeId;

                // First try to find worker by the provided address (could be IP or hostname)
                targetNodeId = current.GetWorkerNodeIdFromAddressOrHostname(targetAddress, targetPort);

                // If not found directly, check if targetAddress is a hostname and try to resolve it
                if (targetNodeId == null && !IPAddress.TryParse(targetAddress, out _))
                {
                    // targetAddress is not a valid IP, try to resolve it as hostname
                    try
                    {
                        var hostEntry = Dns.GetHostEntryAsync(targetAddress).ConfigureAwait(false).GetAwaiter().GetResult();
                        if (hostEntry.AddressList.Length > 0)
                        {
                            // Try each resolved IP address to find one that matches cluster config
                            foreach (var resolvedIp in hostEntry.AddressList)
                            {
                                effectiveAddress = resolvedIp.ToString();
                                targetNodeId = current.GetWorkerNodeIdFromAddressOrHostname(effectiveAddress, targetPort);
                                if (targetNodeId != null)
                                    break; // Found a match in cluster config
                            }
                        }
                    }
                    catch (System.Net.Sockets.SocketException ex)
                    {
                        // Hostname resolution failed (DNS failure, network issue, etc.)
                        logger?.LogWarning(ex, "Failed to resolve hostname {hostname}", targetAddress);
                        pstate = MigrateCmdParseState.HOSTNAME_RESOLUTION_FAILED;
                    }
                    catch (ArgumentException ex)
                    {
                        // Hostname is invalid (e.g., too long or malformed)
                        logger?.LogWarning(ex, "Failed to resolve hostname {hostname} due to invalid format", targetAddress);
                        pstate = MigrateCmdParseState.HOSTNAME_RESOLUTION_FAILED;
                    }
                }

                if (targetNodeId == null && pstate == MigrateCmdParseState.SUCCESS)
                    pstate = MigrateCmdParseState.UNKNOWNTARGET;
            }

            // Add single key if specified
            if (keySlice.Length > 0)
            {
                transferOption = TransferOption.KEYS;