summaryrefslogtreecommitdiffstats
path: root/model/cluster_discovery.go
diff options
context:
space:
mode:
authorCorey Hulen <corey@hulen.com>2017-06-19 08:44:04 -0700
committerGitHub <noreply@github.com>2017-06-19 08:44:04 -0700
commit36f216cb7cb16958d98b3d77e121198596fd2213 (patch)
treeac2a5b79494749b3dffc2f5778092f2529c98d1a /model/cluster_discovery.go
parentfe48987a32fbd600458edd4e81318071ae558ba4 (diff)
downloadchat-36f216cb7cb16958d98b3d77e121198596fd2213.tar.gz
chat-36f216cb7cb16958d98b3d77e121198596fd2213.tar.bz2
chat-36f216cb7cb16958d98b3d77e121198596fd2213.zip
PLT-6080 moving clustering to memberlist (#6499)
* PLT-6080 adding cluster discovery service * Adding memberlist lib * Adding memberlist lib * WIP * WIP * WIP * WIP * Rolling back config changes * Fixing make file * Fixing config for cluster * WIP * Fixing system console for clustering * Fixing default config * Fixing config * Fixing system console for clustering * Tweaking hub setting * Bumping up time * merging vendor dir * Updating vendor dir * Fixing unit test * Fixing bad merge * Remove some testing code * Moving comment * PLT-6868 adding db ping retry * Removing unused loc strings * Adding defer to cancel
Diffstat (limited to 'model/cluster_discovery.go')
-rw-r--r--model/cluster_discovery.go132
1 files changed, 132 insertions, 0 deletions
diff --git a/model/cluster_discovery.go b/model/cluster_discovery.go
new file mode 100644
index 000000000..4b9269656
--- /dev/null
+++ b/model/cluster_discovery.go
@@ -0,0 +1,132 @@
+// Copyright (c) 2017-present Mattermost, Inc. All Rights Reserved.
+// See License.txt for license information.
+
+package model
+
+import (
+ "encoding/json"
+ "io"
+ "os"
+)
+
+const (
+ CDS_OFFLINE_AFTER_MILLIS = 1000 * 60 * 30 // 30 minutes
+ CDS_TYPE_APP = "mattermost_app"
+)
+
+type ClusterDiscovery struct {
+ Id string `json:"id"`
+ Type string `json:"type"`
+ ClusterName string `json:"cluster_name"`
+ Hostname string `json:"hostname"`
+ GossipPort int32 `json:"gossip_port"`
+ Port int32 `json:"port"`
+ CreateAt int64 `json:"create_at"`
+ LastPingAt int64 `json:"last_ping_at"`
+}
+
+func (o *ClusterDiscovery) PreSave() {
+ if o.Id == "" {
+ o.Id = NewId()
+ }
+
+ if o.CreateAt == 0 {
+ o.CreateAt = GetMillis()
+ o.LastPingAt = o.CreateAt
+ }
+}
+
+func (o *ClusterDiscovery) AutoFillHostname() {
+ // attempt to set the hostname from the OS
+ if len(o.Hostname) == 0 {
+ if hn, err := os.Hostname(); err == nil {
+ o.Hostname = hn
+ }
+ }
+}
+
+func (o *ClusterDiscovery) AutoFillIpAddress() {
+ // attempt to set the hostname to the first non-local IP address
+ if len(o.Hostname) == 0 {
+ o.Hostname = GetServerIpAddress()
+ }
+}
+
+func (o *ClusterDiscovery) IsEqual(in *ClusterDiscovery) bool {
+ if in == nil {
+ return false
+ }
+
+ if o.Type != in.Type {
+ return false
+ }
+
+ if o.ClusterName != in.ClusterName {
+ return false
+ }
+
+ if o.Hostname != in.Hostname {
+ return false
+ }
+
+ return true
+}
+
+func FilterClusterDiscovery(vs []*ClusterDiscovery, f func(*ClusterDiscovery) bool) []*ClusterDiscovery {
+ copy := make([]*ClusterDiscovery, 0)
+ for _, v := range vs {
+ if f(v) {
+ copy = append(copy, v)
+ }
+ }
+
+ return copy
+}
+
+func (o *ClusterDiscovery) IsValid() *AppError {
+ if len(o.Id) != 26 {
+ return NewLocAppError("Channel.IsValid", "model.channel.is_valid.id.app_error", nil, "")
+ }
+
+ if len(o.ClusterName) == 0 {
+ return NewLocAppError("ClusterDiscovery.IsValid", "ClusterName must be set", nil, "")
+ }
+
+ if len(o.Type) == 0 {
+ return NewLocAppError("ClusterDiscovery.IsValid", "Type must be set", nil, "")
+ }
+
+ if len(o.Hostname) == 0 {
+ return NewLocAppError("ClusterDiscovery.IsValid", "Hostname must be set", nil, "")
+ }
+
+ if o.CreateAt == 0 {
+ return NewLocAppError("ClusterDiscovery.IsValid", "CreateAt must be set", nil, "")
+ }
+
+ if o.LastPingAt == 0 {
+ return NewLocAppError("ClusterDiscovery.IsValid", "LastPingAt must be set", nil, "")
+ }
+
+ return nil
+}
+
+func (o *ClusterDiscovery) ToJson() string {
+ b, err := json.Marshal(o)
+ if err != nil {
+ return ""
+ }
+
+ return string(b)
+}
+
+func ClusterDiscoveryFromJson(data io.Reader) *ClusterDiscovery {
+ decoder := json.NewDecoder(data)
+ var me ClusterDiscovery
+ err := decoder.Decode(&me)
+ if err == nil {
+ return &me
+ }
+
+ return nil
+}