Skip to content

Commit

Permalink
Merge pull request #1263 from arenadata/develop
Browse files Browse the repository at this point in the history
Release 2021.11.17
  • Loading branch information
acmnu authored Nov 17, 2021
2 parents 27c721e + 29f6e3a commit 9221bc1
Show file tree
Hide file tree
Showing 443 changed files with 108,688 additions and 27,989 deletions.
41 changes: 10 additions & 31 deletions go/adcm/status/handlers.go
Original file line number Diff line number Diff line change
Expand Up @@ -275,16 +275,12 @@ func setHost(h Hub, w http.ResponseWriter, r *http.Request) {
ErrOut4(w, r, "HOST_NOT_FOUND", "unknown host")
return
}
oldClusterStatus := getClusterStatus(h, clusterId)
oldStatus, ok := h.HostStorage.get(ALL, hostId)
res := h.HostStorage.set(ALL, hostId, status)
if oldStatus.Status != status || !ok {
h.EventWS.send2ws(newEventMsg(status, "host", hostId))
}
newClusterStatus := getClusterStatus(h, clusterId)
if oldClusterStatus != newClusterStatus {
h.EventWS.send2ws(newEventMsg(status, "cluster", clusterId))
h.StatusEvent.save_host(h, hostId, clusterId)
clear := func() {
h.StatusEvent.check_host(h, hostId, clusterId)
}
res := h.HostStorage.set(ALL, hostId, status, clear)
h.StatusEvent.check_host(h, hostId, clusterId)
jsonOut3(w, r, "", res)
}

Expand Down Expand Up @@ -321,29 +317,12 @@ func setHostComp(h Hub, w http.ResponseWriter, r *http.Request) {
ErrOut4(w, r, "HC_NOT_FOUND", msg)
return
}

oldCompStatus, _ := getComponentStatus(h, compId)
oldServStatus, _ := getServiceStatus(h, hc.Cluster, hc.Service)
oldClusterStatus := getClusterStatus(h, hc.Cluster)
oldHCStatus, _ := h.HostComponentStorage.get(hostId, compId)

res := h.HostComponentStorage.set(hostId, compId, status)

newCompStatus, _ := getComponentStatus(h, compId)
newServStatus, _ := getServiceStatus(h, hc.Cluster, hc.Service)
newClusterStatus := getClusterStatus(h, hc.Cluster)
if oldHCStatus.Status != status {
h.EventWS.send2ws(newEventMsg4(status, "hostcomponent", hostId, compId))
}
if oldCompStatus.Status != newCompStatus.Status {
h.EventWS.send2ws(newEventMsg(newCompStatus.Status, "component", compId))
}
if oldServStatus.Status != newServStatus.Status {
h.EventWS.send2ws(newEventMsg(newServStatus.Status, "service", hc.Service))
}
if oldClusterStatus != newClusterStatus {
h.EventWS.send2ws(newEventMsg(status, "cluster", hc.Cluster))
h.StatusEvent.save_hc(h, hostId, compId, hc)
clear := func() {
h.StatusEvent.check_hc(h, hostId, compId, hc)
}
res := h.HostComponentStorage.set(hostId, compId, status, clear)
h.StatusEvent.check_hc(h, hostId, compId, hc)
jsonOut3(w, r, "", res)
}

Expand Down
3 changes: 3 additions & 0 deletions go/adcm/status/init.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ type Hub struct {
HostComponentStorage *Storage
ServiceMap *ServiceServer
EventWS *wsHub
StatusEvent *StatusEvent
AdcmApi *AdcmApi
Secrets *SecretConfig
}
Expand Down Expand Up @@ -58,6 +59,8 @@ func Start(secrets *SecretConfig, logFile string, logLevel string) {
hub.AdcmApi.getServiceMap()
}()

hub.StatusEvent = newStatusEvent()

startHTTP(httpPort, hub)
}

Expand Down
20 changes: 12 additions & 8 deletions go/adcm/status/status.go
Original file line number Diff line number Diff line change
Expand Up @@ -40,9 +40,11 @@ type eventMsg struct {
}

type eventDetail struct {
Id string `json:"id,omitempty"`
Type string `json:"type"`
Value interface{} `json:"value"`
Id string `json:"id,omitempty"`
HostId string `json:"host_id,omitempty"`
ComponentId string `json:"component_id,omitempty"`
Type string `json:"type"`
Value interface{} `json:"value"`
}

func (e eventMsg) encode() ([]byte, error) {
Expand All @@ -55,9 +57,11 @@ func newEventMsg4(status int, objType string, objId int, id2 int) eventMsg {
em.Object.Type = objType
em.Object.Id = objId
em.Object.Details = eventDetail{
Type: "status",
Value: strconv.Itoa(status),
Id: strconv.Itoa(id2),
Type: "status",
Value: strconv.Itoa(status),
Id: strconv.Itoa(id2),
HostId: strconv.Itoa(objId),
ComponentId: strconv.Itoa(id2),
}
return em
}
Expand Down Expand Up @@ -164,8 +168,8 @@ func cookClusterStatus(serviceStatus int, hostStatus int) int {
return 0
}

func getClusterStatus(h Hub, clusterId int) int {
func getClusterStatus(h Hub, clusterId int) Status {
serviceStatus, _ := getClusterServiceStatus(h, clusterId)
hostStatus, _ := getClusterHostStatus(h, clusterId)
return cookClusterStatus(serviceStatus, hostStatus)
return Status{Status: cookClusterStatus(serviceStatus, hostStatus)}
}
105 changes: 105 additions & 0 deletions go/adcm/status/status_event.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,105 @@
package status

import (
"fmt"
"sync"
)

type StatusHolder struct {
cluster Status
service Status
component Status
host Status
hostComp Status
}

type StatusEvent struct {
db map[string]StatusHolder
mutex sync.Mutex
}

func newStatusEvent() *StatusEvent {
return &StatusEvent{
db: map[string]StatusHolder{},
}
}

func (se *StatusEvent) save_hc(h Hub, hostId int, compId int, hc ClusterService) {
sh := fill_hc_status(h, hostId, compId, hc)
se.write(fmt.Sprintf("hc.%d.%d", hostId, compId), sh)
se.write(fmt.Sprintf("cluster.%d", hc.Cluster), sh)
}

func (se *StatusEvent) save_host(h Hub, hostId int, clusterId int) {
sh := fill_host_status(h, hostId, clusterId)
se.write(fmt.Sprintf("host.%d", hostId), sh)
se.write(fmt.Sprintf("cluster.%d", clusterId), sh)
}

func (se *StatusEvent) check_hc(h Hub, hostId int, compId int, hc ClusterService) {
key := fmt.Sprintf("hc.%d.%d", hostId, compId)
cluster_key := fmt.Sprintf("cluster.%d", hc.Cluster)
old := se.read(key)
new := fill_hc_status(h, hostId, compId, hc)
if old.hostComp.Status != new.hostComp.Status {
h.EventWS.send2ws(newEventMsg4(new.hostComp.Status, "hostcomponent", hostId, compId))
}
if old.component.Status != new.component.Status {
h.EventWS.send2ws(newEventMsg(new.component.Status, "component", compId))
}
if old.service.Status != new.service.Status {
h.EventWS.send2ws(newEventMsg(new.service.Status, "service", hc.Service))
}
old = se.read(cluster_key)
if old.cluster.Status != new.cluster.Status {
h.EventWS.send2ws(newEventMsg(new.cluster.Status, "cluster", hc.Cluster))
}
se.write(key, new)
se.write(cluster_key, new)
}

func (se *StatusEvent) check_host(h Hub, hostId int, clusterId int) {
key := fmt.Sprintf("host.%d", hostId)
cluster_key := fmt.Sprintf("cluster.%d", clusterId)
old := se.read(key)
new := fill_host_status(h, hostId, clusterId)
if old.host.Status != new.host.Status {
h.EventWS.send2ws(newEventMsg(new.host.Status, "host", hostId))
}
old = se.read(cluster_key)
if old.cluster.Status != new.cluster.Status {
h.EventWS.send2ws(newEventMsg(new.cluster.Status, "cluster", clusterId))
}
se.write(key, new)
se.write(cluster_key, new)
}

func (se *StatusEvent) write(key string, sh StatusHolder) {
se.mutex.Lock()
defer se.mutex.Unlock()
se.db[key] = sh
}

func (se *StatusEvent) read(key string) StatusHolder {
se.mutex.Lock()
defer se.mutex.Unlock()
return se.db[key]
}

func fill_hc_status(h Hub, hostId int, compId int, hc ClusterService) StatusHolder {
sh := StatusHolder{}
sh.component, _ = getComponentStatus(h, compId)
sh.service, _ = getServiceStatus(h, hc.Cluster, hc.Service)
sh.cluster = getClusterStatus(h, hc.Cluster)
sh.hostComp, _ = h.HostComponentStorage.get(hostId, compId)
return sh
}

func fill_host_status(h Hub, hostId int, clusterId int) StatusHolder {
ClusterStatus := getClusterStatus(h, clusterId)
HostStatus, _ := h.HostStorage.get(ALL, hostId)
return StatusHolder{
cluster: ClusterStatus,
host: HostStatus,
}
}
18 changes: 11 additions & 7 deletions go/adcm/status/storage.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,11 +19,12 @@ type Status struct {
}

type storageReq struct {
command string
key1 int
key2 int
val int
counter int
command string
key1 int
key2 int
val int
counter int
clearFunc func()
}

type storageResp struct {
Expand Down Expand Up @@ -90,6 +91,9 @@ func (s *Storage) run() {
s.out <- storageResp{code: v}
case cmdClear:
s.dbMap.clear(c.key1, c.key2, c.counter)
if c.clearFunc != nil {
go c.clearFunc()
}
case cmdPure:
s.dbMap = s.dbMap.create()
s.out <- storageResp{ok: true}
Expand All @@ -107,8 +111,8 @@ func (s *Storage) run() {

// Interface

func (s *Storage) set(key1 int, key2 int, val int) int {
req := storageReq{command: cmdSet, key1: key1, key2: key2, val: val}
func (s *Storage) set(key1 int, key2 int, val int, clear func()) int {
req := storageReq{command: cmdSet, key1: key1, key2: key2, val: val, clearFunc: clear}
s.in <- req
resp := <-s.out
return resp.code
Expand Down
4 changes: 2 additions & 2 deletions python/ansible/plugins/action/adcm_hc.py
Original file line number Diff line number Diff line change
Expand Up @@ -68,9 +68,9 @@ class ActionModule(ActionBase):

def run(self, tmp=None, task_vars=None):
super().run(tmp, task_vars)
msg = 'You can modify hc only in cluster or service context'
msg = 'You can modify hc only in cluster, service or component context'
cluster_id = get_object_id_from_context(
task_vars, 'cluster_id', 'cluster', 'service', err_msg=msg
task_vars, 'cluster_id', 'cluster', 'service', 'component', err_msg=msg
)
job_id = task_vars['job']['id']
ops = self._task.args['operations']
Expand Down
83 changes: 79 additions & 4 deletions python/api/cluster/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@
from api.api_views import ListView, PageView, PageViewAdd, InterfaceView, DetailViewDelete
from api.api_views import create, update, check_obj, GenericAPIPermView
from cm.errors import AdcmEx
from cm.models import Cluster, HostComponent, Prototype
from cm.models import Cluster, Host, HostComponent, Prototype
from cm.models import ClusterObject, Upgrade, ClusterBind
from . import serializers

Expand Down Expand Up @@ -235,18 +235,93 @@ def post(self, request, cluster_id, upgrade_id):
return create(serializer, upgrade_id=int(upgrade_id), obj=cluster)


class StatusList(GenericAPIPermView):
class StatusList(GenericAPIPermView, InterfaceView):
queryset = HostComponent.objects.all()
serializer_class = serializers.StatusSerializer

def ui_status(self, cluster, host_component):
cluster_map = cm.status_api.get_object_map(cluster, 'cluster')

def get_status(key, obj_id):
if cluster_map is None:
return 32
if str(obj_id) in cluster_map[key]:
return cluster_map[key][str(obj_id)]['status']
else:
return 0

service_map = {}
for hc in host_component:
if hc.service.id not in service_map:
service_map[hc.service.id] = {'service': hc.service, 'hc': {}}
if hc.component.id not in service_map[hc.service.id]['hc']:
service_map[hc.service.id]['hc'][hc.component.id] = {
'comp': hc.component,
'hosts': [],
}
service_map[hc.service.id]['hc'][hc.component.id]['hosts'].append(hc.host)

# convert map to list
service_list = []
for srv in service_map.values():
hc_list = []
for hc in srv['hc'].values():
host_comp_list = []
for host in hc['hosts']:
host_comp_list.append(
{
'id': host.id,
'name': host.fqdn,
'status': cm.status_api.get_host_comp_status(host, hc['comp']),
}
)
hc_list.append(
{
'id': hc['comp'].id,
'name': hc['comp'].display_name,
'status': cm.status_api.get_component_status(hc['comp']),
'hosts': host_comp_list,
}
)
service_list.append(
{
'id': srv['service'].id,
'name': srv['service'].display_name,
'status': get_status('services', srv['service'].id),
'hc': hc_list,
}
)

host_list = []
for host in Host.obj.filter(cluster=cluster):
host_list.append(
{
'id': host.id,
'name': host.fqdn,
'status': get_status('hosts', host.id),
}
)

return {
'name': cluster.name,
'status': 32 if cluster_map is None else cluster_map.get('status', 0),
'chilren': {
'hosts': host_list,
'services': service_list,
},
}

def get(self, request, cluster_id):
"""
Show all hosts and components in a specified cluster
"""
cluster = check_obj(Cluster, cluster_id)
obj = self.get_queryset().filter(cluster=cluster)
serializer = self.serializer_class(obj, many=True, context={'request': request})
return Response(serializer.data)
if self.for_ui(request):
return Response(self.ui_status(cluster, obj))
else:
serializer = self.serializer_class(obj, many=True, context={'request': request})
return Response(serializer.data)


class HostComponentList(GenericAPIPermView, InterfaceView):
Expand Down
Loading

0 comments on commit 9221bc1

Please sign in to comment.