github.com/juju/juju@v0.0.0-20240430160146-1752b71fcf00/state/allcollections.go (about) 1 // Copyright 2012-2015 Canonical Ltd. 2 // Licensed under the AGPLv3, see LICENCE file for details. 3 4 package state 5 6 import ( 7 "github.com/juju/mgo/v3" 8 9 "github.com/juju/juju/state/bakerystorage" 10 "github.com/juju/juju/state/cloudimagemetadata" 11 ) 12 13 // allCollections should be the single source of truth for information about 14 // any collection we use. It's broken up into 4 main sections: 15 // 16 // - infrastructure: we really don't have any business touching these once 17 // we've created them. They should have the rawAccess attribute set, so that 18 // multiModelRunner will consider them forbidden. 19 // 20 // - global: these hold information external to models. They may include 21 // model metadata, or references; but they're generally not relevant 22 // from the perspective of a given model. 23 // 24 // - local (in opposition to global; and for want of a better term): these 25 // hold information relevant *within* specific models (machines, 26 // applications, relations, settings, bookkeeping, etc) and should generally be 27 // read via an modelStateCollection, and written via a multiModelRunner. This is 28 // the most common form of collection, and the above access should usually 29 // be automatic via Database.Collection and Database.Runner. 30 // 31 // - raw-access: there's certainly data that's a poor fit for mgo/txn. Most 32 // forms of logs, for example, will benefit both from the speedy insert and 33 // worry-free bulk deletion; so raw-access collections are fine. Just don't 34 // try to run transactions that reference them. 35 // 36 // Please do not use collections not referenced here; and when adding new 37 // collections, please document them, and make an effort to put them in an 38 // appropriate section. 39 func allCollections() CollectionSchema { 40 result := CollectionSchema{ 41 42 // Infrastructure collections 43 // ========================== 44 45 globalClockC: { 46 global: true, 47 rawAccess: true, 48 }, 49 txnsC: { 50 // This collection is used exclusively by mgo/txn to record transactions. 51 global: true, 52 rawAccess: true, 53 indexes: []mgo.Index{{ 54 // The "s" field is used in queries 55 // by mgo/txn.Runner.ResumeAll. 56 Key: []string{"s"}, 57 }}, 58 }, 59 60 // ------------------ 61 62 // Global collections 63 // ================== 64 65 // This collection holds the details of the controllers hosting, well, 66 // everything in state. 67 controllersC: {global: true}, 68 69 // This collection holds the details of the HA-ness of controllers. 70 controllerNodesC: {}, 71 72 // This collection is used by the controllers to coordinate binary 73 // upgrades and schema migrations. 74 upgradeInfoC: {global: true}, 75 76 // This collection holds a convenient representation of the content of 77 // the simplestreams data source pointing to binaries required by juju. 78 // 79 // Tools metadata is per-model, to allow multiple revisions of tools to 80 // be uploaded to different models without affecting other models. 81 toolsmetadataC: {}, 82 83 // This collection holds model information; in particular its 84 // Life and its UUID. 85 modelsC: {global: true}, 86 87 // This collection holds references to entities owned by a 88 // model. We use this to determine whether or not we can safely 89 // destroy empty models. 90 modelEntityRefsC: {global: true}, 91 92 // This collection is holds the parameters for model migrations. 93 migrationsC: { 94 global: true, 95 indexes: []mgo.Index{{ 96 Key: []string{"model-uuid", "-attempt"}, 97 }}, 98 }, 99 100 // This collection tracks the progress of model migrations. 101 migrationsStatusC: {global: true}, 102 103 // This collection records the model migrations which 104 // are currently in progress. It is used to ensure that only 105 // one model migration document exists per model. 106 migrationsActiveC: {global: true}, 107 108 // This collection tracks migration progress reports from the 109 // migration minions. 110 migrationsMinionSyncC: {global: true}, 111 112 // This collection holds user information that's not specific to any 113 // one model. 114 usersC: { 115 global: true, 116 }, 117 118 // This collection holds users that are relative to controllers. 119 controllerUsersC: { 120 global: true, 121 }, 122 123 // This collection holds the last time the user connected to the API server. 124 userLastLoginC: { 125 global: true, 126 rawAccess: true, 127 }, 128 129 // This collection is used as a unique key restraint. The _id field is 130 // a concatenation of multiple fields that form a compound index, 131 // allowing us to ensure users cannot have the same name for two 132 // different models at a time. 133 usermodelnameC: {global: true}, 134 135 // This collection holds cloud definitions. 136 cloudsC: {global: true}, 137 138 // This collection holds users' cloud credentials. 139 cloudCredentialsC: { 140 global: true, 141 indexes: []mgo.Index{{ 142 Key: []string{"owner", "cloud"}, 143 }}, 144 }, 145 146 // This collection holds settings from various sources which 147 // are inherited and then forked by new models. 148 globalSettingsC: {global: true}, 149 150 // This collection holds workload metrics reported by certain charms 151 // for passing onward to other tools. 152 metricsC: { 153 global: true, 154 indexes: []mgo.Index{{ 155 Key: []string{"model-uuid", "sent"}, 156 }}, 157 }, 158 159 // This collection holds persistent state for the metrics manager. 160 metricsManagerC: {global: true}, 161 162 // This collection was deprecated before multi-model support 163 // was implemented. 164 actionresultsC: {global: true}, 165 166 // This collection holds storage items for a macaroon bakery. 167 bakeryStorageItemsC: { 168 global: true, 169 indexes: bakerystorage.MongoIndexes(), 170 }, 171 172 // This collection is basically a standard SQL intersection table; it 173 // references the global records of the users allowed access to a 174 // given operation. 175 permissionsC: { 176 global: true, 177 indexes: []mgo.Index{{ 178 Key: []string{"object-global-key", "subject-global-key"}, 179 }}, 180 }, 181 182 // This collection holds information cached by autocert certificate 183 // acquisition. 184 autocertCacheC: { 185 global: true, 186 rawAccess: true, 187 }, 188 189 // This collection holds the last time the model user connected 190 // to the model. 191 modelUserLastConnectionC: { 192 rawAccess: true, 193 }, 194 195 // ----------------- 196 197 // Local collections 198 // ================= 199 200 // This collection holds users related to a model and will be used as one 201 // of the intersection axis of permissionsC 202 modelUsersC: { 203 indexes: []mgo.Index{{ 204 Key: []string{"model-uuid", "user"}, 205 }}, 206 }, 207 208 // This collection contains governors that prevent certain kinds of 209 // changes from being accepted. 210 blocksC: {}, 211 212 // This collection is used for internal bookkeeping; certain complex 213 // or tedious state changes are deferred by recording a cleanup doc 214 // for later handling. 215 cleanupsC: { 216 indexes: []mgo.Index{{ 217 Key: []string{"model-uuid"}, 218 }}, 219 }, 220 221 // This collection contains incrementing integers, subdivided by name, 222 // to ensure various IDs aren't reused. 223 sequenceC: { 224 indexes: []mgo.Index{{ 225 Key: []string{"model-uuid"}, 226 }}, 227 }, 228 229 // ----- 230 231 // These collections hold information associated with applications. 232 charmsC: { 233 indexes: []mgo.Index{{ 234 Key: []string{"model-uuid"}, 235 }, { 236 Key: []string{"bundlesha256"}, 237 }}, 238 }, 239 applicationsC: { 240 indexes: []mgo.Index{{ 241 Key: []string{"model-uuid", "name"}, 242 }}, 243 }, 244 unitsC: { 245 indexes: []mgo.Index{{ 246 Key: []string{"model-uuid", "application"}, 247 }, { 248 Key: []string{"model-uuid", "principal"}, 249 }, { 250 Key: []string{"model-uuid", "machineid"}, 251 }, { 252 Key: []string{"model-uuid", "name"}, 253 }}, 254 }, 255 unitStatesC: { 256 indexes: []mgo.Index{{ 257 Key: []string{"model-uuid"}, 258 }}, 259 }, 260 minUnitsC: {}, 261 262 // This collection holds documents that indicate units which are queued 263 // to be assigned to machines. It is used exclusively by the 264 // AssignUnitWorker. 265 assignUnitC: {}, 266 267 // meterStatusC is the collection used to store meter status information. 268 meterStatusC: { 269 indexes: []mgo.Index{{ 270 Key: []string{"model-uuid"}, 271 }}, 272 }, 273 274 // These collections hold reference counts which are used 275 // by the nsRefcounts struct. 276 refcountsC: {}, // Per model. 277 globalRefcountsC: { 278 global: true, 279 }, 280 281 relationsC: { 282 indexes: []mgo.Index{{ 283 Key: []string{"model-uuid", "endpoints.applicationname", "endpoints.relation.name"}, 284 }, { 285 Key: []string{"model-uuid", "id"}, // id here is the relation id not the doc _id 286 }}, 287 }, 288 relationScopesC: { 289 indexes: []mgo.Index{{ 290 Key: []string{"model-uuid", "key", "departing"}, 291 }}, 292 }, 293 294 // Stores Docker image resource details 295 dockerResourcesC: {}, 296 297 // ----- 298 299 // These collections hold information associated with machines. 300 containerRefsC: { 301 indexes: []mgo.Index{{ 302 Key: []string{"model-uuid"}, 303 }}, 304 }, 305 instanceDataC: { 306 indexes: []mgo.Index{{ 307 Key: []string{"model-uuid", "machineid"}, 308 }, { 309 Key: []string{"model-uuid", "instanceid"}, 310 }}, 311 }, 312 machinesC: { 313 indexes: []mgo.Index{{ 314 Key: []string{"model-uuid", "machineid"}, 315 }}, 316 }, 317 rebootC: { 318 indexes: []mgo.Index{{ 319 Key: []string{"model-uuid", "machineid"}, 320 }}, 321 }, 322 sshHostKeysC: {}, 323 324 // This collection contains information from removed machines 325 // that needs to be cleaned up in the provider. 326 machineRemovalsC: { 327 indexes: []mgo.Index{{ 328 Key: []string{"model-uuid"}, 329 }}, 330 }, 331 332 // this collection contains machine update locks whose existence indicates 333 // that a particular machine in the process of performing a series upgrade. 334 machineUpgradeSeriesLocksC: { 335 indexes: []mgo.Index{{ 336 Key: []string{"model-uuid", "machineid"}, 337 }}, 338 }, 339 340 // ----- 341 342 // These collections hold information associated with storage. 343 blockDevicesC: { 344 indexes: []mgo.Index{{ 345 Key: []string{"model-uuid", "machineid"}, 346 }}, 347 }, 348 filesystemsC: { 349 indexes: []mgo.Index{{ 350 Key: []string{"model-uuid", "storageid"}, 351 }, { 352 Key: []string{"model-uuid", "machineid"}, 353 }}, 354 }, 355 filesystemAttachmentsC: {}, 356 storageInstancesC: { 357 indexes: []mgo.Index{{ 358 Key: []string{"model-uuid", "owner"}, 359 }}, 360 }, 361 storageAttachmentsC: { 362 indexes: []mgo.Index{{ 363 Key: []string{"model-uuid", "storageid"}, 364 }, { 365 Key: []string{"model-uuid", "unitid"}, 366 }}, 367 }, 368 volumesC: { 369 indexes: []mgo.Index{{ 370 Key: []string{"model-uuid", "storageid"}, 371 }, { 372 Key: []string{"model-uuid", "hostid"}, 373 }}, 374 }, 375 volumeAttachmentsC: { 376 indexes: []mgo.Index{{ 377 Key: []string{"model-uuid", "hostid"}, 378 }, { 379 Key: []string{"model-uuid", "volumeid"}, 380 }}, 381 }, 382 volumeAttachmentPlanC: { 383 indexes: []mgo.Index{{ 384 Key: []string{"model-uuid"}, 385 }}, 386 }, 387 388 // ----- 389 390 providerIDsC: { 391 indexes: []mgo.Index{{ 392 Key: []string{"model-uuid"}, 393 }}, 394 }, 395 spacesC: { 396 indexes: []mgo.Index{ 397 {Key: []string{"model-uuid", "spaceid"}}, 398 {Key: []string{"model-uuid", "name"}}, 399 }, 400 }, 401 subnetsC: { 402 indexes: []mgo.Index{{ 403 Key: []string{"model-uuid"}, 404 }}, 405 }, 406 linkLayerDevicesC: { 407 indexes: []mgo.Index{{ 408 Key: []string{"model-uuid", "machine-id"}, 409 }}, 410 }, 411 ipAddressesC: { 412 indexes: []mgo.Index{{ 413 Key: []string{"model-uuid", "machine-id", "device-name"}, 414 }}, 415 }, 416 endpointBindingsC: { 417 indexes: []mgo.Index{{ 418 Key: []string{"model-uuid"}, 419 }}, 420 }, 421 openedPortsC: { 422 indexes: []mgo.Index{{ 423 Key: []string{"model-uuid"}, 424 }}, 425 }, 426 427 // ----- 428 429 // These collections hold information associated with actions. 430 actionsC: { 431 indexes: []mgo.Index{{ 432 Key: []string{"model-uuid", "name"}, 433 }, { 434 Key: []string{"model-uuid", "operation"}, 435 }}, 436 }, 437 actionNotificationsC: { 438 indexes: []mgo.Index{{ 439 Key: []string{"model-uuid"}, 440 }}, 441 }, 442 operationsC: { 443 indexes: []mgo.Index{{ 444 Key: []string{"model-uuid", "_id"}, 445 }}, 446 }, 447 448 // ----- 449 450 // This collection holds information associated with charm payloads. 451 payloadsC: { 452 indexes: []mgo.Index{{ 453 Key: []string{"model-uuid", "unitid"}, 454 }, { 455 Key: []string{"model-uuid", "name"}, 456 }}, 457 }, 458 459 // This collection holds information associated with charm resources. 460 // See resource/persistence/mongo.go, where it should never have 461 // been put in the first place. 462 "resources": {}, 463 // see vendor/github.com/juju/blobstore/v2/resourcecatalog.go 464 // This shouldn't need to be declared here, but we need to allocate the 465 // collection before a TXN tries to insert it. 466 "storedResources": {}, 467 468 // ----- 469 470 // The remaining non-global collections share the property of being 471 // relevant to multiple other kinds of entities, and are thus generally 472 // indexed by globalKey(). This is unhelpfully named in this context -- 473 // it's meant to imply "global within an model", because it was 474 // named before multi-model support. 475 476 // This collection holds user annotations for various entities. They 477 // shouldn't be written or interpreted by juju. 478 annotationsC: { 479 indexes: []mgo.Index{{ 480 Key: []string{"model-uuid"}, 481 }}, 482 }, 483 484 // This collection in particular holds an astounding number of 485 // different sorts of data: application config settings by charm version, 486 // unit relation settings, model config, etc etc etc. 487 settingsC: { 488 indexes: []mgo.Index{{ 489 Key: []string{"model-uuid"}, 490 }}, 491 }, 492 493 // The generations collection holds data about 494 // active and completed "next" model generations. 495 generationsC: { 496 indexes: []mgo.Index{{ 497 Key: []string{"model-uuid", "completed"}, 498 }}, 499 }, 500 501 constraintsC: { 502 indexes: []mgo.Index{{ 503 Key: []string{"model-uuid"}, 504 }}, 505 }, 506 storageConstraintsC: { 507 indexes: []mgo.Index{{ 508 Key: []string{"model-uuid"}, 509 }}, 510 }, 511 deviceConstraintsC: {}, 512 statusesC: { 513 indexes: []mgo.Index{{ 514 Key: []string{"model-uuid", "_id"}, 515 }}, 516 }, 517 statusesHistoryC: { 518 rawAccess: true, 519 indexes: []mgo.Index{{ 520 Key: []string{"model-uuid", "globalkey", "updated"}, 521 }, { 522 // used for migration and model-specific pruning 523 Key: []string{"model-uuid", "-updated", "-_id"}, 524 }, { 525 // used for global pruning (after size check) 526 Key: []string{"-updated"}, 527 }}, 528 }, 529 530 // This collection holds information about cloud image metadata. 531 cloudimagemetadataC: { 532 global: true, 533 indexes: cloudimagemetadata.MongoIndexes(), 534 }, 535 536 // Cross model relations collections. 537 applicationOffersC: { 538 indexes: []mgo.Index{ 539 {Key: []string{"model-uuid", "_id"}}, 540 {Key: []string{"model-uuid", "application-name"}}, 541 }, 542 }, 543 offerConnectionsC: { 544 indexes: []mgo.Index{ 545 {Key: []string{"model-uuid", "offer-uuid"}}, 546 {Key: []string{"model-uuid", "username"}}, 547 }, 548 }, 549 remoteApplicationsC: { 550 indexes: []mgo.Index{{ 551 Key: []string{"model-uuid"}, 552 }}, 553 }, 554 // remoteEntitiesC holds information about entities involved in 555 // cross-model relations. 556 remoteEntitiesC: { 557 indexes: []mgo.Index{{ 558 Key: []string{"model-uuid", "token"}, 559 }}, 560 }, 561 // externalControllersC holds connection information for other 562 // controllers hosting models involved in cross-model relations. 563 externalControllersC: { 564 global: true, 565 }, 566 // relationNetworksC holds required ingress or egress cidrs for remote relations. 567 relationNetworksC: { 568 indexes: []mgo.Index{{ 569 Key: []string{"model-uuid"}, 570 }}, 571 }, 572 573 // podSpecsC holds the CAAS pod specifications, 574 // for applications. 575 podSpecsC: { 576 indexes: []mgo.Index{{ 577 Key: []string{"model-uuid"}, 578 }}, 579 }, 580 581 // cloudContainersC holds the CAAS container (pod) information 582 // for units, eg address, ports. 583 cloudContainersC: { 584 indexes: []mgo.Index{{ 585 Key: []string{"model-uuid", "provider-id"}, 586 }}, 587 }, 588 589 // cloudServicesC holds the CAAS service information 590 // eg addresses. 591 cloudServicesC: { 592 indexes: []mgo.Index{{ 593 Key: []string{"model-uuid"}, 594 }}, 595 }, 596 597 secretMetadataC: { 598 indexes: []mgo.Index{{ 599 Key: []string{"owner-tag", "label", "model-uuid"}, 600 }}, 601 }, 602 603 secretRevisionsC: { 604 indexes: []mgo.Index{{ 605 Key: []string{"revision", "_id"}, 606 }}, 607 }, 608 609 secretConsumersC: { 610 indexes: []mgo.Index{{ 611 Key: []string{"consumer-tag", "label", "model-uuid"}, 612 }}, 613 }, 614 615 secretRemoteConsumersC: { 616 indexes: []mgo.Index{{ 617 Key: []string{"consumer-tag", "model-uuid"}, 618 }}, 619 }, 620 621 secretPermissionsC: { 622 indexes: []mgo.Index{{ 623 Key: []string{"subject-tag", "scope-tag", "model-uuid"}, 624 }}, 625 }, 626 627 secretRotateC: { 628 indexes: []mgo.Index{{ 629 Key: []string{"owner-tag", "model-uuid"}, 630 }}, 631 }, 632 633 secretBackendsC: { 634 global: true, 635 indexes: []mgo.Index{{ 636 Key: []string{"name"}, 637 }}, 638 }, 639 640 secretBackendsRotateC: { 641 global: true, 642 indexes: []mgo.Index{{ 643 Key: []string{"model-uuid"}, 644 }}, 645 }, 646 647 // ---------------------- 648 649 // Raw-access collections 650 // ====================== 651 652 // metrics; status-history; logs; ..? 653 654 } 655 return result 656 } 657 658 // These constants are used to avoid sprinkling the package with any more 659 // magic strings. If a collection deserves documentation, please document 660 // it in allCollections, above; and please keep this list sorted for easy 661 // inspection. 662 const ( 663 actionNotificationsC = "actionnotifications" 664 actionresultsC = "actionresults" 665 actionsC = "actions" 666 annotationsC = "annotations" 667 autocertCacheC = "autocertCache" 668 assignUnitC = "assignUnits" 669 bakeryStorageItemsC = "bakeryStorageItems" 670 blockDevicesC = "blockdevices" 671 blocksC = "blocks" 672 charmsC = "charms" 673 cleanupsC = "cleanups" 674 cloudimagemetadataC = "cloudimagemetadata" 675 cloudsC = "clouds" 676 cloudContainersC = "cloudcontainers" 677 cloudServicesC = "cloudservices" 678 cloudCredentialsC = "cloudCredentials" 679 constraintsC = "constraints" 680 containerRefsC = "containerRefs" 681 controllersC = "controllers" 682 controllerNodesC = "controllerNodes" 683 controllerUsersC = "controllerusers" 684 dockerResourcesC = "dockerResources" 685 filesystemAttachmentsC = "filesystemAttachments" 686 filesystemsC = "filesystems" 687 globalClockC = "globalclock" 688 globalRefcountsC = "globalRefcounts" 689 globalSettingsC = "globalSettings" 690 instanceDataC = "instanceData" 691 machinesC = "machines" 692 machineRemovalsC = "machineremovals" 693 machineUpgradeSeriesLocksC = "machineUpgradeSeriesLocks" 694 meterStatusC = "meterStatus" 695 metricsC = "metrics" 696 metricsManagerC = "metricsmanager" 697 minUnitsC = "minunits" 698 migrationsActiveC = "migrations.active" 699 migrationsC = "migrations" 700 migrationsMinionSyncC = "migrations.minionsync" 701 migrationsStatusC = "migrations.status" 702 modelUserLastConnectionC = "modelUserLastConnection" 703 modelUsersC = "modelusers" 704 modelsC = "models" 705 modelEntityRefsC = "modelEntityRefs" 706 openedPortsC = "openedPorts" 707 operationsC = "operations" 708 payloadsC = "payloads" 709 permissionsC = "permissions" 710 podSpecsC = "podSpecs" 711 providerIDsC = "providerIDs" 712 rebootC = "reboot" 713 relationScopesC = "relationscopes" 714 relationsC = "relations" 715 sequenceC = "sequence" 716 applicationsC = "applications" 717 endpointBindingsC = "endpointbindings" 718 settingsC = "settings" 719 generationsC = "generations" 720 refcountsC = "refcounts" 721 resourcesC = "resources" 722 sshHostKeysC = "sshhostkeys" 723 spacesC = "spaces" 724 statusesC = "statuses" 725 statusesHistoryC = "statuseshistory" 726 storageAttachmentsC = "storageattachments" 727 storageConstraintsC = "storageconstraints" 728 deviceConstraintsC = "deviceConstraints" 729 storageInstancesC = "storageinstances" 730 subnetsC = "subnets" 731 linkLayerDevicesC = "linklayerdevices" 732 ipAddressesC = "ip.addresses" 733 toolsmetadataC = "toolsmetadata" 734 txnsC = "txns" 735 unitsC = "units" 736 unitStatesC = "unitstates" 737 upgradeInfoC = "upgradeInfo" 738 userLastLoginC = "userLastLogin" 739 usermodelnameC = "usermodelname" 740 usersC = "users" 741 volumeAttachmentsC = "volumeattachments" 742 volumeAttachmentPlanC = "volumeattachmentplan" 743 volumesC = "volumes" 744 745 // Cross model relations 746 applicationOffersC = "applicationOffers" 747 remoteApplicationsC = "remoteApplications" 748 offerConnectionsC = "applicationOfferConnections" 749 remoteEntitiesC = "remoteEntities" 750 externalControllersC = "externalControllers" 751 relationNetworksC = "relationNetworks" 752 753 // Secrets 754 secretMetadataC = "secretMetadata" 755 secretRevisionsC = "secretRevisions" 756 secretConsumersC = "secretConsumers" 757 secretRemoteConsumersC = "secretRemoteConsumers" 758 secretPermissionsC = "secretPermissions" 759 secretRotateC = "secretRotate" 760 secretBackendsC = "secretBackends" 761 secretBackendsRotateC = "secretBackendsRotate" 762 ) 763 764 // watcherIgnoreList contains all the collections in mongo that should not be watched by the 765 // TxnWatcher. 766 var watcherIgnoreList = []string{ 767 bakeryStorageItemsC, 768 sequenceC, 769 refcountsC, 770 statusesHistoryC, 771 }