Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Multi tenant #2917

Merged
merged 20 commits into from
Aug 13, 2020
Merged

Multi tenant #2917

merged 20 commits into from
Aug 13, 2020

Conversation

daneryl
Copy link
Collaborator

@daneryl daneryl commented May 13, 2020

fixes #2393

basic set up for testing multi-tenant

  • create tenants with diferent names on the shared_db, by default: 'uwazi_shared_db'
mongo localhost/uwazi_shared_db --eval 'db.tenants.insert({ name : "tenant1", dbName : "tenant1", indexName: "tenant1" });'
mongo localhost/uwazi_shared_db --eval 'db.tenants.insert({ name : "tenant2", dbName : "tenant2", indexName: "tenant2" });'
  • create databases for the newly created tenants:
yarn blank-state tenant1
yarn blank-state tenant2
  • set up an nginx proxy with this basic config file
server {
  listen       3001;

  location / {
    proxy_set_header Upgrade $http_upgrade;
    proxy_set_header Connection "upgrade";
    proxy_set_header tenant tenant1;
    proxy_pass http://localhost:3000;
     ## this is in case you use docker for mac 
     ## proxy_pass http://docker.for.mac.localhost:3000;
  }
}

server {
  listen       3002;

  location / {
    proxy_set_header Upgrade $http_upgrade;
    proxy_set_header Connection "upgrade";
    proxy_set_header tenant tenant2;
    proxy_pass http://localhost:3000;
     ## this is in case you use docker for mac 
     ## proxy_pass http://docker.for.mac.localhost:3000;
  }
}

  • start uwazi with multi_tenant activated
MULTI_TENANT=true yarn hot

now you should be able to acces tenant1 on localhost:3001 and tenant2 on 3002, tenants can be added on the fly (with the same commands above) also while uwazi is running without restarting the node process, when MULTI_TENANT is not set the instance should behave exactly like it behaves now on development for backward compatibility.

scripts like reindex and migrate still require to set up the proper env vars with the names of the db and elastic indexes.

PR checklist:

  • Update READ.me ?
  • Update API documentation ?

QA checklist:

  • Smoke test the functionality described in the issue
  • UI responsiveness
  • Test for side effects
  • Cross browser testing
  • Code review

@daneryl daneryl marked this pull request as draft May 13, 2020 06:20
@daneryl daneryl force-pushed the multi-tenant branch 15 times, most recently from 5a58f47 to 4dc7e6c Compare May 19, 2020 07:53
@daneryl daneryl force-pushed the multi-tenant branch 13 times, most recently from 82a568c to 3099b74 Compare May 22, 2020 07:12
@daneryl
Copy link
Collaborator Author

daneryl commented Aug 4, 2020

@daneryl, maybe is this the expected behavior? when we navigate in the same browser with different tenant in each tab, the user is emptied of the state (example). This is not happening with independent browsers.

@mfacar this is due to cookies not being port-specific the same thing happens if you use the development branch with 2 instances in different ports, we solve this by having a proxy nginx configured with diferent domain names per instance.

@daneryl, after adding a tenant on the fly, the application crashes with this message:
uncaught exception or unhandled rejection, Node process finished !!
Error: tenant does not exists at Tenants.current (/Users/mercyfalconi/Documents/projects/uwazi/app/api/tenants/tenantContext.ts:61:13) at MultiTenantMongooseModel.dbForCurrentTenant (/Users/mercyfalconi/Documents/projects/uwazi/app/api/odm/MultiTenantMongooseModel.ts:20:50)

@mfacar sorry i forgot, to be able to add tenants on the fly mongo should be running as a replica set, quick replica set locally:

mongod --replSet rs0
mongo localhost --eval 'rs.initiate()'

with that, you should be able to add a tenant on the fly.
It is true that the instance is crashing when requesting for a tenant that does not exists, seems to easy to take down and i think the uwazi process should be resilient to this (i have fixed this, uwazi process should not crash anymore but will return the same error), there is 2 cases that i know of that crash the process:

  • tenant does not exists
  • tenant exists but db for tenant does not

app/api/socketio/specs/socketMultiTenant.spec.ts Outdated Show resolved Hide resolved
app/api/utils/staticFilesMiddleware.ts Outdated Show resolved Hide resolved
app/api/attachments/specs/routes.spec.js Outdated Show resolved Hide resolved
app/api/i18n/specs/routes.spec.js Outdated Show resolved Hide resolved
app/api/migrations/migrate.ts Show resolved Hide resolved
app/api/socketio/setupSockets.ts Show resolved Hide resolved
app/api/sync/specs/uploadRoute.spec.ts Outdated Show resolved Hide resolved
app/api/utils/specs/staticFilesMiddleware.spec.ts Outdated Show resolved Hide resolved
app/api/utils/specs/staticFilesMiddleware.spec.ts Outdated Show resolved Hide resolved
@fnocetti
Copy link
Contributor

fnocetti commented Aug 5, 2020

@daneryl Uwazi seems to be ignoring the MULTI_TENANT flag, even when starting up with MUTLI_TENANT=false yarn hot it is working on multitenant mode (I can access tenant specific instances, e.g. localhost:3001)

@daneryl
Copy link
Collaborator Author

daneryl commented Aug 10, 2020

@daneryl Uwazi seems to be ignoring the MULTI_TENANT flag, even when starting up with MUTLI_TENANT=false yarn hot it is working on multitenant mode (I can access tenant specific instances, e.g. localhost:3001)

this is true, the initial idea is that there is no single-tenant mode, uwazi is always multi-tenant, the reason for the flag to exist is to temporary have some features that can not work when using uwazi as multi-tenant, basically this flag is to remove backward compatibility on some features, maybe we should rename the flag ?

@daneryl daneryl force-pushed the multi-tenant branch 3 times, most recently from 1f5c339 to 7725d94 Compare August 13, 2020 10:43
@daneryl daneryl merged commit 290a8d8 into development Aug 13, 2020
@daneryl daneryl deleted the multi-tenant branch August 13, 2020 15:37
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Multi-tenant uwazi core
5 participants