github.com/kardianos/nomad@v0.1.3-0.20151022182107-b13df73ee850/nomad/state/schema.go (about)

     1  package state
     2  
     3  import (
     4  	"fmt"
     5  
     6  	"github.com/hashicorp/go-memdb"
     7  )
     8  
     9  // stateStoreSchema is used to return the schema for the state store
    10  func stateStoreSchema() *memdb.DBSchema {
    11  	// Create the root DB schema
    12  	db := &memdb.DBSchema{
    13  		Tables: make(map[string]*memdb.TableSchema),
    14  	}
    15  
    16  	// Collect all the schemas that are needed
    17  	schemas := []func() *memdb.TableSchema{
    18  		indexTableSchema,
    19  		nodeTableSchema,
    20  		jobTableSchema,
    21  		evalTableSchema,
    22  		allocTableSchema,
    23  	}
    24  
    25  	// Add each of the tables
    26  	for _, schemaFn := range schemas {
    27  		schema := schemaFn()
    28  		if _, ok := db.Tables[schema.Name]; ok {
    29  			panic(fmt.Sprintf("duplicate table name: %s", schema.Name))
    30  		}
    31  		db.Tables[schema.Name] = schema
    32  	}
    33  	return db
    34  }
    35  
    36  // indexTableSchema is used for
    37  func indexTableSchema() *memdb.TableSchema {
    38  	return &memdb.TableSchema{
    39  		Name: "index",
    40  		Indexes: map[string]*memdb.IndexSchema{
    41  			"id": &memdb.IndexSchema{
    42  				Name:         "id",
    43  				AllowMissing: false,
    44  				Unique:       true,
    45  				Indexer: &memdb.StringFieldIndex{
    46  					Field:     "Key",
    47  					Lowercase: true,
    48  				},
    49  			},
    50  		},
    51  	}
    52  }
    53  
    54  // nodeTableSchema returns the MemDB schema for the nodes table.
    55  // This table is used to store all the client nodes that are registered.
    56  func nodeTableSchema() *memdb.TableSchema {
    57  	return &memdb.TableSchema{
    58  		Name: "nodes",
    59  		Indexes: map[string]*memdb.IndexSchema{
    60  			// Primary index is used for node management
    61  			// and simple direct lookup. ID is required to be
    62  			// unique.
    63  			"id": &memdb.IndexSchema{
    64  				Name:         "id",
    65  				AllowMissing: false,
    66  				Unique:       true,
    67  				Indexer: &memdb.StringFieldIndex{
    68  					Field:     "ID",
    69  					Lowercase: true,
    70  				},
    71  			},
    72  		},
    73  	}
    74  }
    75  
    76  // jobTableSchema returns the MemDB schema for the jobs table.
    77  // This table is used to store all the jobs that have been submitted.
    78  func jobTableSchema() *memdb.TableSchema {
    79  	return &memdb.TableSchema{
    80  		Name: "jobs",
    81  		Indexes: map[string]*memdb.IndexSchema{
    82  			// Primary index is used for job management
    83  			// and simple direct lookup. ID is required to be
    84  			// unique.
    85  			"id": &memdb.IndexSchema{
    86  				Name:         "id",
    87  				AllowMissing: false,
    88  				Unique:       true,
    89  				Indexer: &memdb.StringFieldIndex{
    90  					Field:     "ID",
    91  					Lowercase: true,
    92  				},
    93  			},
    94  			"type": &memdb.IndexSchema{
    95  				Name:         "type",
    96  				AllowMissing: false,
    97  				Unique:       false,
    98  				Indexer: &memdb.StringFieldIndex{
    99  					Field:     "Type",
   100  					Lowercase: false,
   101  				},
   102  			},
   103  		},
   104  	}
   105  }
   106  
   107  // evalTableSchema returns the MemDB schema for the eval table.
   108  // This table is used to store all the evaluations that are pending
   109  // or recently completed.
   110  func evalTableSchema() *memdb.TableSchema {
   111  	return &memdb.TableSchema{
   112  		Name: "evals",
   113  		Indexes: map[string]*memdb.IndexSchema{
   114  			// Primary index is used for direct lookup.
   115  			"id": &memdb.IndexSchema{
   116  				Name:         "id",
   117  				AllowMissing: false,
   118  				Unique:       true,
   119  				Indexer: &memdb.UUIDFieldIndex{
   120  					Field: "ID",
   121  				},
   122  			},
   123  
   124  			// Job index is used to lookup allocations by job
   125  			"job": &memdb.IndexSchema{
   126  				Name:         "job",
   127  				AllowMissing: false,
   128  				Unique:       false,
   129  				Indexer: &memdb.StringFieldIndex{
   130  					Field:     "JobID",
   131  					Lowercase: true,
   132  				},
   133  			},
   134  		},
   135  	}
   136  }
   137  
   138  // allocTableSchema returns the MemDB schema for the allocation table.
   139  // This table is used to store all the task allocations between task groups
   140  // and nodes.
   141  func allocTableSchema() *memdb.TableSchema {
   142  	return &memdb.TableSchema{
   143  		Name: "allocs",
   144  		Indexes: map[string]*memdb.IndexSchema{
   145  			// Primary index is a UUID
   146  			"id": &memdb.IndexSchema{
   147  				Name:         "id",
   148  				AllowMissing: false,
   149  				Unique:       true,
   150  				Indexer: &memdb.UUIDFieldIndex{
   151  					Field: "ID",
   152  				},
   153  			},
   154  
   155  			// Node index is used to lookup allocations by node
   156  			"node": &memdb.IndexSchema{
   157  				Name:         "node",
   158  				AllowMissing: true, // Missing is allow for failed allocations
   159  				Unique:       false,
   160  				Indexer: &memdb.StringFieldIndex{
   161  					Field:     "NodeID",
   162  					Lowercase: true,
   163  				},
   164  			},
   165  
   166  			// Job index is used to lookup allocations by job
   167  			"job": &memdb.IndexSchema{
   168  				Name:         "job",
   169  				AllowMissing: false,
   170  				Unique:       false,
   171  				Indexer: &memdb.StringFieldIndex{
   172  					Field:     "JobID",
   173  					Lowercase: true,
   174  				},
   175  			},
   176  
   177  			// Eval index is used to lookup allocations by eval
   178  			"eval": &memdb.IndexSchema{
   179  				Name:         "eval",
   180  				AllowMissing: false,
   181  				Unique:       false,
   182  				Indexer: &memdb.UUIDFieldIndex{
   183  					Field: "EvalID",
   184  				},
   185  			},
   186  		},
   187  	}
   188  }