github.com/jmitchell/nomad@v0.1.3-0.20151007230021-7ab84c2862d8/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  		},
    95  	}
    96  }
    97  
    98  // evalTableSchema returns the MemDB schema for the eval table.
    99  // This table is used to store all the evaluations that are pending
   100  // or recently completed.
   101  func evalTableSchema() *memdb.TableSchema {
   102  	return &memdb.TableSchema{
   103  		Name: "evals",
   104  		Indexes: map[string]*memdb.IndexSchema{
   105  			// Primary index is used for direct lookup.
   106  			"id": &memdb.IndexSchema{
   107  				Name:         "id",
   108  				AllowMissing: false,
   109  				Unique:       true,
   110  				Indexer: &memdb.UUIDFieldIndex{
   111  					Field: "ID",
   112  				},
   113  			},
   114  
   115  			// Job index is used to lookup allocations by job
   116  			"job": &memdb.IndexSchema{
   117  				Name:         "job",
   118  				AllowMissing: false,
   119  				Unique:       false,
   120  				Indexer: &memdb.StringFieldIndex{
   121  					Field:     "JobID",
   122  					Lowercase: true,
   123  				},
   124  			},
   125  		},
   126  	}
   127  }
   128  
   129  // allocTableSchema returns the MemDB schema for the allocation table.
   130  // This table is used to store all the task allocations between task groups
   131  // and nodes.
   132  func allocTableSchema() *memdb.TableSchema {
   133  	return &memdb.TableSchema{
   134  		Name: "allocs",
   135  		Indexes: map[string]*memdb.IndexSchema{
   136  			// Primary index is a UUID
   137  			"id": &memdb.IndexSchema{
   138  				Name:         "id",
   139  				AllowMissing: false,
   140  				Unique:       true,
   141  				Indexer: &memdb.UUIDFieldIndex{
   142  					Field: "ID",
   143  				},
   144  			},
   145  
   146  			// Node index is used to lookup allocations by node
   147  			"node": &memdb.IndexSchema{
   148  				Name:         "node",
   149  				AllowMissing: true, // Missing is allow for failed allocations
   150  				Unique:       false,
   151  				Indexer: &memdb.StringFieldIndex{
   152  					Field:     "NodeID",
   153  					Lowercase: true,
   154  				},
   155  			},
   156  
   157  			// Job index is used to lookup allocations by job
   158  			"job": &memdb.IndexSchema{
   159  				Name:         "job",
   160  				AllowMissing: false,
   161  				Unique:       false,
   162  				Indexer: &memdb.StringFieldIndex{
   163  					Field:     "JobID",
   164  					Lowercase: true,
   165  				},
   166  			},
   167  
   168  			// Eval index is used to lookup allocations by eval
   169  			"eval": &memdb.IndexSchema{
   170  				Name:         "eval",
   171  				AllowMissing: false,
   172  				Unique:       false,
   173  				Indexer: &memdb.UUIDFieldIndex{
   174  					Field: "EvalID",
   175  				},
   176  			},
   177  		},
   178  	}
   179  }