Skip to content

Commit

Permalink
WIP
Browse files Browse the repository at this point in the history
Signed-off-by: hamistao <[email protected]>
  • Loading branch information
hamistao committed Nov 28, 2024
1 parent a0ca414 commit 66c3dbe
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 1 deletion.
6 changes: 5 additions & 1 deletion lxd/instance/drivers/driver_lxc.go
Original file line number Diff line number Diff line change
Expand Up @@ -8044,7 +8044,9 @@ func (d *lxc) IsPrivileged() bool {

// IsRunning returns if instance is running.
func (d *lxc) IsRunning() bool {
return d.isRunningStatusCode(d.statusCode())
s := d.statusCode()
fmt.Println(s.String())
return d.isRunningStatusCode(s)
}

// CanMigrate returns whether the instance can be migrated.
Expand Down Expand Up @@ -8142,6 +8144,8 @@ func (d *lxc) NextIdmap() (*idmap.IdmapSet, error) {
// statusCode returns instance status code.
func (d *lxc) statusCode() api.StatusCode {
// If instance is running on a remote cluster member, we cannot determine instance state.
fmt.Println(d.state.ServerName)
fmt.Println(d.Location())
if d.state.ServerName != d.Location() {
return api.Error
}
Expand Down
14 changes: 14 additions & 0 deletions lxd/instances.go
Original file line number Diff line number Diff line change
Expand Up @@ -455,20 +455,25 @@ func instancesOnDisk(s *state.State) ([]instance.Instance, error) {
}

func instancesShutdown(instances []instance.Instance) {
fmt.Println("instance shutdown")

sort.Sort(instanceStopList(instances))

// Limit shutdown concurrency to number of instances or number of CPU cores (which ever is less).
var wg sync.WaitGroup
instShutdownCh := make(chan instance.Instance)
maxConcurrent := runtime.NumCPU()
instCount := len(instances)
fmt.Printf("%d instances\n", len(instances))
if instCount < maxConcurrent {
maxConcurrent = instCount
}

for i := 0; i < maxConcurrent; i++ {
go func(instShutdownCh <-chan instance.Instance) {
for inst := range instShutdownCh {
fmt.Printf("shutting down %s\n", inst.Name())

// Determine how long to wait for the instance to shutdown cleanly.
timeoutSeconds := 30
value, ok := inst.ExpandedConfig()["boot.host_shutdown_timeout"]
Expand All @@ -478,11 +483,15 @@ func instancesShutdown(instances []instance.Instance) {

err := inst.Shutdown(time.Second * time.Duration(timeoutSeconds))
if err != nil {
fmt.Printf("failed shutting down %s\n", inst.Name())
logger.Warn("Failed shutting down instance, forcefully stopping", logger.Ctx{"project": inst.Project().Name, "instance": inst.Name(), "err": err})
err = inst.Stop(false)
if err != nil {
fmt.Printf("failed shutting down %s forcefully\n", inst.Name())
logger.Warn("Failed forcefully stopping instance", logger.Ctx{"project": inst.Project().Name, "instance": inst.Name(), "err": err})
}
} else {
fmt.Printf("succeeded shutting down %s\n", inst.Name())
}

if inst.ID() > 0 {
Expand All @@ -499,8 +508,11 @@ func instancesShutdown(instances []instance.Instance) {

var currentBatchPriority int
for i, inst := range instances {
fmt.Printf("looping through %s\n", inst.Name())

// Skip stopped instances.
if !inst.IsRunning() {
fmt.Printf("skipping %s\n", inst.Name())
continue
}

Expand All @@ -516,6 +528,8 @@ func instancesShutdown(instances []instance.Instance) {
}

wg.Add(1)

fmt.Printf("adding %s to channel\n", inst.Name())
instShutdownCh <- inst
}

Expand Down
3 changes: 3 additions & 0 deletions test/suites/clustering.sh
Original file line number Diff line number Diff line change
Expand Up @@ -1894,6 +1894,9 @@ test_clustering_shutdown_nodes() {
wait "${daemon_pid1}"

# Container foo shouldn't be running anymore
stat "/proc/${instance_pid}"
sleep 20
stat "/proc/${instance_pid}"
[ ! -e "/proc/${instance_pid}" ]

teardown_clustering_netns
Expand Down

0 comments on commit 66c3dbe

Please sign in to comment.