Skip to content

Commit 832bd52

Browse files
authored
feat: replace Pipeliner with lua (#3846)
Signed-off-by: Gaius <gaius.qi@gmail.com>
1 parent 522074b commit 832bd52

File tree

6 files changed

+458
-1292
lines changed

6 files changed

+458
-1292
lines changed

scheduler/resource/persistentcache/host_manager.go

Lines changed: 243 additions & 82 deletions
Original file line numberDiff line numberDiff line change
@@ -119,7 +119,7 @@ func (h *hostManager) Load(ctx context.Context, hostID string) (*Host, bool) {
119119
}
120120

121121
// Set boolean fields from raw host.
122-
diableShared, err := strconv.ParseBool(rawHost["disable_shared"])
122+
disableShared, err := strconv.ParseBool(rawHost["disable_shared"])
123123
if err != nil {
124124
log.Errorf("parsing disable shared failed: %v", err)
125125
return nil, false
@@ -435,7 +435,7 @@ func (h *hostManager) Load(ctx context.Context, hostID string) (*Host, bool) {
435435
int32(port),
436436
int32(downloadPort),
437437
uint64(schedulerClusterID),
438-
diableShared,
438+
disableShared,
439439
pkgtypes.ParseHostType(rawHost["type"]),
440440
cpu,
441441
memory,
@@ -451,76 +451,212 @@ func (h *hostManager) Load(ctx context.Context, hostID string) (*Host, bool) {
451451

452452
// Store sets host.
453453
func (h *hostManager) Store(ctx context.Context, host *Host) error {
454-
if _, err := h.rdb.TxPipelined(ctx, func(pipe redis.Pipeliner) error {
455-
if _, err := pipe.HSet(ctx,
456-
pkgredis.MakePersistentCacheHostKeyInScheduler(h.config.Manager.SchedulerClusterID, host.ID),
457-
"id", host.ID,
458-
"type", host.Type.Name(),
459-
"hostname", host.Hostname,
460-
"ip", host.IP,
461-
"port", host.Port,
462-
"download_port", host.DownloadPort,
463-
"disable_shared", host.DisableShared,
464-
"os", host.OS,
465-
"platform", host.Platform,
466-
"platform_family", host.PlatformFamily,
467-
"platform_version", host.PlatformVersion,
468-
"kernel_version", host.KernelVersion,
469-
"cpu_logical_count", host.CPU.LogicalCount,
470-
"cpu_physical_count", host.CPU.PhysicalCount,
471-
"cpu_percent", host.CPU.Percent,
472-
"cpu_processe_percent", host.CPU.ProcessPercent,
473-
"cpu_times_user", host.CPU.Times.User,
474-
"cpu_times_system", host.CPU.Times.System,
475-
"cpu_times_idle", host.CPU.Times.Idle,
476-
"cpu_times_nice", host.CPU.Times.Nice,
477-
"cpu_times_iowait", host.CPU.Times.Iowait,
478-
"cpu_times_irq", host.CPU.Times.Irq,
479-
"cpu_times_softirq", host.CPU.Times.Softirq,
480-
"cpu_times_steal", host.CPU.Times.Steal,
481-
"cpu_times_guest", host.CPU.Times.Guest,
482-
"cpu_times_guest_nice", host.CPU.Times.GuestNice,
483-
"memory_total", host.Memory.Total,
484-
"memory_available", host.Memory.Available,
485-
"memory_used", host.Memory.Used,
486-
"memory_used_percent", host.Memory.UsedPercent,
487-
"memory_processe_used_percent", host.Memory.ProcessUsedPercent,
488-
"memory_free", host.Memory.Free,
489-
"network_tcp_connection_count", host.Network.TCPConnectionCount,
490-
"network_upload_tcp_connection_count", host.Network.UploadTCPConnectionCount,
491-
"network_location", host.Network.Location,
492-
"network_idc", host.Network.IDC,
493-
"network_download_rate", host.Network.DownloadRate,
494-
"network_download_rate_limit", host.Network.DownloadRateLimit,
495-
"network_upload_rate", host.Network.UploadRate,
496-
"network_upload_rate_limit", host.Network.UploadRateLimit,
497-
"disk_total", host.Disk.Total,
498-
"disk_free", host.Disk.Free,
499-
"disk_used", host.Disk.Used,
500-
"disk_used_percent", host.Disk.UsedPercent,
501-
"disk_inodes_total", host.Disk.InodesTotal,
502-
"disk_inodes_used", host.Disk.InodesUsed,
503-
"disk_inodes_free", host.Disk.InodesFree,
504-
"disk_inodes_used_percent", host.Disk.InodesUsedPercent,
505-
"disk_write_bandwidth", host.Disk.WriteBandwidth,
506-
"disk_read_bandwidth", host.Disk.ReadBandwidth,
507-
"build_git_version", host.Build.GitVersion,
508-
"build_git_commit", host.Build.GitCommit,
509-
"build_go_version", host.Build.GoVersion,
510-
"build_platform", host.Build.Platform,
511-
"scheduler_cluster_id", host.SchedulerClusterID,
512-
"announce_interval", host.AnnounceInterval.Nanoseconds(),
513-
"created_at", host.CreatedAt.Format(time.RFC3339),
514-
"updated_at", host.UpdatedAt.Format(time.RFC3339)).Result(); err != nil {
515-
return err
516-
}
517-
518-
if _, err := pipe.SAdd(ctx, pkgredis.MakePersistentCacheHostsInScheduler(h.config.Manager.SchedulerClusterID), host.ID).Result(); err != nil {
519-
return err
520-
}
521-
522-
return nil
523-
}); err != nil {
454+
// Define the Lua script as a string.
455+
const storeHostScript = `
456+
-- Extract keys and arguments
457+
local host_key = KEYS[1] -- Key for the host hash
458+
local hosts_set_key = KEYS[2] -- Key for the set of hosts
459+
460+
-- Extract host fields from arguments
461+
local host_id = ARGV[1]
462+
local host_type = ARGV[2]
463+
local hostname = ARGV[3]
464+
local ip = ARGV[4]
465+
local port = ARGV[5]
466+
local download_port = ARGV[6]
467+
local disable_shared = tonumber(ARGV[7])
468+
local os = ARGV[8]
469+
local platform = ARGV[9]
470+
local platform_family = ARGV[10]
471+
local platform_version = ARGV[11]
472+
local kernel_version = ARGV[12]
473+
local cpu_logical_count = ARGV[13]
474+
local cpu_physical_count = ARGV[14]
475+
local cpu_percent = ARGV[15]
476+
local cpu_process_percent = ARGV[16]
477+
local cpu_times_user = ARGV[17]
478+
local cpu_times_system = ARGV[18]
479+
local cpu_times_idle = ARGV[19]
480+
local cpu_times_nice = ARGV[20]
481+
local cpu_times_iowait = ARGV[21]
482+
local cpu_times_irq = ARGV[22]
483+
local cpu_times_softirq = ARGV[23]
484+
local cpu_times_steal = ARGV[24]
485+
local cpu_times_guest = ARGV[25]
486+
local cpu_times_guest_nice = ARGV[26]
487+
local memory_total = ARGV[27]
488+
local memory_available = ARGV[28]
489+
local memory_used = ARGV[29]
490+
local memory_used_percent = ARGV[30]
491+
local memory_process_used_percent = ARGV[31]
492+
local memory_free = ARGV[32]
493+
local network_tcp_connection_count = ARGV[33]
494+
local network_upload_tcp_connection_count = ARGV[34]
495+
local network_location = ARGV[35]
496+
local network_idc = ARGV[36]
497+
local network_download_rate = ARGV[37]
498+
local network_download_rate_limit = ARGV[38]
499+
local network_upload_rate = ARGV[39]
500+
local network_upload_rate_limit = ARGV[40]
501+
local disk_total = ARGV[41]
502+
local disk_free = ARGV[42]
503+
local disk_used = ARGV[43]
504+
local disk_used_percent = ARGV[44]
505+
local disk_inodes_total = ARGV[45]
506+
local disk_inodes_used = ARGV[46]
507+
local disk_inodes_free = ARGV[47]
508+
local disk_inodes_used_percent = ARGV[48]
509+
local disk_write_bandwidth = ARGV[49]
510+
local disk_read_bandwidth = ARGV[50]
511+
local build_git_version = ARGV[51]
512+
local build_git_commit = ARGV[52]
513+
local build_go_version = ARGV[53]
514+
local build_platform = ARGV[54]
515+
local scheduler_cluster_id = ARGV[55]
516+
local announce_interval = ARGV[56]
517+
local created_at = ARGV[57]
518+
local updated_at = ARGV[58]
519+
520+
-- Perform HSET operation
521+
redis.call("HSET", host_key,
522+
"id", host_id,
523+
"type", host_type,
524+
"hostname", hostname,
525+
"ip", ip,
526+
"port", port,
527+
"download_port", download_port,
528+
"disable_shared", disable_shared,
529+
"os", os,
530+
"platform", platform,
531+
"platform_family", platform_family,
532+
"platform_version", platform_version,
533+
"kernel_version", kernel_version,
534+
"cpu_logical_count", cpu_logical_count,
535+
"cpu_physical_count", cpu_physical_count,
536+
"cpu_percent", cpu_percent,
537+
"cpu_processe_percent", cpu_process_percent,
538+
"cpu_times_user", cpu_times_user,
539+
"cpu_times_system", cpu_times_system,
540+
"cpu_times_idle", cpu_times_idle,
541+
"cpu_times_nice", cpu_times_nice,
542+
"cpu_times_iowait", cpu_times_iowait,
543+
"cpu_times_irq", cpu_times_irq,
544+
"cpu_times_softirq", cpu_times_softirq,
545+
"cpu_times_steal", cpu_times_steal,
546+
"cpu_times_guest", cpu_times_guest,
547+
"cpu_times_guest_nice", cpu_times_guest_nice,
548+
"memory_total", memory_total,
549+
"memory_available", memory_available,
550+
"memory_used", memory_used,
551+
"memory_used_percent", memory_used_percent,
552+
"memory_processe_used_percent", memory_process_used_percent,
553+
"memory_free", memory_free,
554+
"network_tcp_connection_count", network_tcp_connection_count,
555+
"network_upload_tcp_connection_count", network_upload_tcp_connection_count,
556+
"network_location", network_location,
557+
"network_idc", network_idc,
558+
"network_download_rate", network_download_rate,
559+
"network_download_rate_limit", network_download_rate_limit,
560+
"network_upload_rate", network_upload_rate,
561+
"network_upload_rate_limit", network_upload_rate_limit,
562+
"disk_total", disk_total,
563+
"disk_free", disk_free,
564+
"disk_used", disk_used,
565+
"disk_used_percent", disk_used_percent,
566+
"disk_inodes_total", disk_inodes_total,
567+
"disk_inodes_used", disk_inodes_used,
568+
"disk_inodes_free", disk_inodes_free,
569+
"disk_inodes_used_percent", disk_inodes_used_percent,
570+
"disk_write_bandwidth", disk_write_bandwidth,
571+
"disk_read_bandwidth", disk_read_bandwidth,
572+
"build_git_version", build_git_version,
573+
"build_git_commit", build_git_commit,
574+
"build_go_version", build_go_version,
575+
"build_platform", build_platform,
576+
"scheduler_cluster_id", scheduler_cluster_id,
577+
"announce_interval", announce_interval,
578+
"created_at", created_at,
579+
"updated_at", updated_at)
580+
581+
-- Perform SADD operation
582+
redis.call("SADD", hosts_set_key, host_id)
583+
584+
return true
585+
`
586+
587+
// Create a new Redis script.
588+
script := redis.NewScript(storeHostScript)
589+
590+
// Prepare keys.
591+
keys := []string{
592+
pkgredis.MakePersistentCacheHostKeyInScheduler(h.config.Manager.SchedulerClusterID, host.ID),
593+
pkgredis.MakePersistentCacheHostsInScheduler(h.config.Manager.SchedulerClusterID),
594+
}
595+
596+
// Prepare arguments.
597+
args := []interface{}{
598+
host.ID,
599+
host.Type.Name(),
600+
host.Hostname,
601+
host.IP,
602+
host.Port,
603+
host.DownloadPort,
604+
host.DisableShared,
605+
host.OS,
606+
host.Platform,
607+
host.PlatformFamily,
608+
host.PlatformVersion,
609+
host.KernelVersion,
610+
host.CPU.LogicalCount,
611+
host.CPU.PhysicalCount,
612+
host.CPU.Percent,
613+
host.CPU.ProcessPercent,
614+
host.CPU.Times.User,
615+
host.CPU.Times.System,
616+
host.CPU.Times.Idle,
617+
host.CPU.Times.Nice,
618+
host.CPU.Times.Iowait,
619+
host.CPU.Times.Irq,
620+
host.CPU.Times.Softirq,
621+
host.CPU.Times.Steal,
622+
host.CPU.Times.Guest,
623+
host.CPU.Times.GuestNice,
624+
host.Memory.Total,
625+
host.Memory.Available,
626+
host.Memory.Used,
627+
host.Memory.UsedPercent,
628+
host.Memory.ProcessUsedPercent,
629+
host.Memory.Free,
630+
host.Network.TCPConnectionCount,
631+
host.Network.UploadTCPConnectionCount,
632+
host.Network.Location,
633+
host.Network.IDC,
634+
host.Network.DownloadRate,
635+
host.Network.DownloadRateLimit,
636+
host.Network.UploadRate,
637+
host.Network.UploadRateLimit,
638+
host.Disk.Total,
639+
host.Disk.Free,
640+
host.Disk.Used,
641+
host.Disk.UsedPercent,
642+
host.Disk.InodesTotal,
643+
host.Disk.InodesUsed,
644+
host.Disk.InodesFree,
645+
host.Disk.InodesUsedPercent,
646+
host.Disk.WriteBandwidth,
647+
host.Disk.ReadBandwidth,
648+
host.Build.GitVersion,
649+
host.Build.GitCommit,
650+
host.Build.GoVersion,
651+
host.Build.Platform,
652+
host.SchedulerClusterID,
653+
host.AnnounceInterval.Nanoseconds(),
654+
host.CreatedAt.Format(time.RFC3339),
655+
host.UpdatedAt.Format(time.RFC3339),
656+
}
657+
658+
// Execute the script.
659+
if err := script.Run(ctx, h.rdb, keys, args...).Err(); err != nil {
524660
host.Log.Errorf("store host failed: %v", err)
525661
return err
526662
}
@@ -530,19 +666,44 @@ func (h *hostManager) Store(ctx context.Context, host *Host) error {
530666

531667
// Delete deletes host by a key.
532668
func (h *hostManager) Delete(ctx context.Context, hostID string) error {
669+
// Define the Lua script as a string.
670+
const deleteHostScript = `
671+
-- Extract keys
672+
local host_key = KEYS[1] -- Key for the host hash
673+
local hosts_set_key = KEYS[2] -- Key for the set of hosts
674+
675+
-- Extract arguments
676+
local host_id = ARGV[1]
677+
678+
-- Perform DEL operation to delete the host hash
679+
redis.call("DEL", host_key)
680+
681+
-- Perform SREM operation to remove the host ID from the set
682+
redis.call("SREM", hosts_set_key, host_id)
683+
684+
return true
685+
`
686+
533687
log := logger.WithHostID(hostID)
534-
if _, err := h.rdb.TxPipelined(ctx, func(pipe redis.Pipeliner) error {
535-
if _, err := pipe.Del(ctx, pkgredis.MakePersistentCacheHostKeyInScheduler(h.config.Manager.SchedulerClusterID, hostID)).Result(); err != nil {
536-
return err
537-
}
538688

539-
if _, err := pipe.SRem(ctx, pkgredis.MakePersistentCacheHostsInScheduler(h.config.Manager.SchedulerClusterID), hostID).Result(); err != nil {
540-
return err
541-
}
689+
// Create a new Redis script.
690+
script := redis.NewScript(deleteHostScript)
542691

543-
return nil
544-
}); err != nil {
545-
log.Errorf("store host failed: %v", err)
692+
// Prepare keys.
693+
keys := []string{
694+
pkgredis.MakePersistentCacheHostKeyInScheduler(h.config.Manager.SchedulerClusterID, hostID),
695+
pkgredis.MakePersistentCacheHostsInScheduler(h.config.Manager.SchedulerClusterID),
696+
}
697+
698+
// Prepare arguments.
699+
args := []interface{}{
700+
hostID,
701+
}
702+
703+
// Execute the script.
704+
err := script.Run(ctx, h.rdb, keys, args...).Err()
705+
if err != nil {
706+
log.Errorf("delete host failed: %v", err)
546707
return err
547708
}
548709

0 commit comments

Comments
 (0)