In large services, solving a problem using machine learning means performing only part of the work. Embedding ML-models is not so easy, and building around them CI /CD-processes is even more difficult. At the Yandex conference
“Data & Science: An Application Program”
YouDo, head of data science at YouDo, told about how to manage the life cycle of models, set up additional training and retraining processes, develop scalable microservices, and much more.
- Let's start with introductory. There is a data scientist, he writes some code in Jupyter Notebook, does feature-engineering, cross-validation, trains models. Growing soon. But at some point he understands: in order to bring the company a business value, he has to seal the solution somewhere in production, into some kind of mythical production, which causes us a lot of problems. Notebook, which we sawed, in production, in most cases, do not send. And the question arises: how to ship this code inside a laptop to a service. In most cases, you need to write a service that has an API. Or they communicate through PubSub, through queues. 3r3335. When we make recommendations, we often need to train models and retrain them. This process must be monitored. At the same time, it is always necessary to check with the tests both the code itself and the models, so that at one moment our model does not go crazy and does not always begin to predict toes. It also needs to be checked on real users through AB-tests - what we have done better or at least not worse. How do we approach code? We have GitLab. All our code is sawn into many small libraries that solve a specific domain problem. At the same time, it is a separate GitLab project, Git-version control and the GitFlow branching model. We use things like pre-commit hooks — so that we cannot commit code that doesn't satisfy our stat tests. And the tests themselves, unit tests. We use for them the property based testing approach. Usually, when you write tests, you mean that you have a test function and the arguments that you create with your hands, some examples, and what values your test function returns. It is not comfortable. The code is inflated, for many, in principle, laziness to write it. As a result, we have a lot of code uncovered by tests. Property based testing implies that all your arguments have some distribution. Let's do phasing, and many times sample all our arguments from these distributions, call the tested function with these arguments and check the result of this function for some properties. As a result, we have much less code, and at the same time there are many more tests. What is GitFlow? This is a branching model, which means that you have two main branches - develop and master, where the production ready-code is located, and all development is done in the develop branch, where all new features come from feature-branches. That is, each feature is a new feature brunch, while the feature brunch must be short-lived, and for good, also covered by feature toggle. We then make a release, from dev we transfer the changes to master and hang the version tag of our library or service on it. 3r366. We do development, we saw some feature, we push it on GitLab, we create merge request from a feature branch in maidens. Triggers are triggered, we run tests, if everything is ok, we can hold them up. But we are not merged, but someone from the team. It revises the code, and thus increases the buzz factor. This section of the code is already known by two people. As a result, if someone gets hit by a bus, someone already knows what he is doing. 3r375. At the same time, continuous integration for libraries usually looks like tests for any changes. And if we release, it's also publishing to the private PyPI server of our package. 3r3384. Further we can collect it in pipelines. For this we use the Luigi library. It works with such an entity as a task, which has an output, where the artifact created during the task is stored. There is a task parameter, which parameterizes the business logic that it executes, identifies the task and its output. At the same time, a task always has requirements that expose other tasks. When we start a task, all its dependencies are checked by checking its output. If an output exists, our dependency does not start. If the artifact is missing on some storage, it is launched. This forms a pipeline, directed cyclic graph. 3r33939. All parameters identify business logic. In doing so, they identify the artifact. It is always a date with some kind of granularity, sensitivity, or a week, day, hour, three hours. If we train some kind of model, Luigi task always has hyper parameters of this task, they leak into the artifact that we produce, the hyper parameters are reflected in the name artifacts. Thus, we essentially version all intermediate data sets and final artifacts, and they are never overwritten, always upend only on storage, and storage appears HDFS and S3 private, which sees artifacts of some models or something else in the final artifacts . And the entire code of the pipeline lies in the project of the service in the repository to which it belongs. 3r3102. This needs to be covered up somehow. HashiCorp comes to the aid of the stack, we use Terraform for declaring the infrastructure in the form of a code, Vault for managing secrets, there all passwords, appearances to the database. Consul - discovery service, distributed key value storage, which can be used for configuration. And also Consul does health checks on your nodes and your services, checking them for availability. And - Nomad. This is a system for orchestrating, sharing your services and some batch jobs. 3r3115. How do we use it? There is a Luigi pipeline, we will pack it in the Docker container, drop it into the Nomad batch or the periodic batch job. The batch job is something done, ended, and if everything is successful - everything is okay, we can manually flush it again. But if something went wrong, Nomad would retreat it until it exhausted it, or it would end successfully. Periodic batch job is exactly the same, it only works on a schedule. There is a problem. When we deploy a container to any orchestration system, we need to indicate how much memory is needed for this container, CPU, or memory. If we have a pipeline that runs for three hours, two hours of this consumes 10 GB of RAM, 1 hour - 70 GB. If we exceed the limit that we gave him, the Docker daemon comes and kills the Dockers and (nrzb.) W2w2w20. We do not want to catch out of memory all the time, so we need to specify all 70 GB of memory peak load. But there is a problem, all 70 GB will be allocated for three hours and not accessible by any other job. Therefore, we went the other way. Our whole Luigi Pipeline does not run some kind of business logic in it, it starts just a set of dice in Nomad, the so-called parameterized jobs. In fact, this is an analogue Server (nrzb.) W2w2w21. Functions, AVS Lambda, who knows. When we make a library, we deploy through CI all our code in the form of parameterized jobs, that is, a container with some parameters. Suppose, Lite JBM Classifier, it has a parameter for the path to the input data for training, hyper-parameters of models and the path to the output artifacts. All this is registered in Nomad, and then from the Luigi pipeline we can pull all these Nomad Jobs through the API, and at the same time Luigi makes sure not to run the same task many times. Suppose we have the same text processing. There are 10 conditional models, and we do not want to re-start text processing every time. It will be launched only once, and at the same time the result will be reused every time. And at the same time it all works distributed, we can run a giant grid search on a large cluster, just have time to drop the iron. 3r33140. We have an artifact, we must somehow arrange it as a service. Services expose either HTTP API, or communicate through queues. In this example, this is the HTTP API, the simplest example. At the same time, communication with the service, or our service communicates with other services via HTTP. The JSON API validates the JSON scheme. At the service itself, a JSON object is always described in the documentation for its API and the scheme of this object. But not always all fields of JSON of the object are needed, therefore, the validation of consumer driven contracts, the validation of this scheme, communication takes place through the pattern circuit breaker, in order not to allow our distributed system to fail due to cascade failures. At the same time, the service should set HTTP health check so that Consul can come and check the availability of this service. At the same time, Nomad is able to do so that there is a service three hellchecks in a row zafililit, he can restart the service to help him. The service writes all its logs in JSON format. We use JSON logging driver and Elastics stack, at every point FileBit simply takes all the JSON logs, throws them into the log stash, from there they get into Elastic, we can analyze to KBan. At the same time, we do not use logs for the collection of metrics and building dashboards, this is inefficient, we use the Prometheus entoring system for this, we have a process to create templates for each dashboard service, and we can analyze technical metrics that are produced by the service. Moreover, if something has gone wrong, we receive alerts, but in most cases this is not enough. We come to the aid of Sentry, this is the thing for the analysis of incidents. In fact, we intercept all the error logs with the Sentry handler and push them into Sentry. And then there is a detailed traceback, there is all the information about what environment the service was in, what version, which functions were called by which arguments and which variables in this scope were, with what values. All configs, all this is visible, and it helps a lot to quickly understand what happened and fix the error. As a result, the service looks something like this. Separate GitLab project, pipeline code, test code, service code itself, a bunch of different configs, Nomad, CI configs, API documentation, commit hooks and more. 3r3r1616. CI, when we make a release, we do it like this: build the container, run tests, throw clustering on staging, run test contract of our service there, conduct load testing to verify that we have a prediction not too slow and keep the load that wears . If everything is okay, we will deploy this service to production. And here there are two ways: we can slow down the pipeline, if it is a periodic batch job, it works somewhere in the background and produces artifacts, or it triggers some kind of pipeline, it trains some kind of modelka, after that we understand that everything is okay and deploy service. 3r33175. What else happens in this case? I said that in the development of feature-branches there is such a paradigm as feature toggles. Fichi in an amicable way, you need to cover up with some toggles in order to just cut down a feature in a fight if something went wrong. We can then collect all the features into release trains, and even if the features are unfinished, we can deploy them. Just the feature toggle will be turned off. Since we are all data scientists, we also want to do AV tests. Let's say we replaced LI GBM with CatBoost. We want to check this, but at the same time the AV test is managed with reference to some userID. Feature toggle binds to the userID, and thus passes the AV test. We need to check these metrics here. All services are deployed in Nomad. We have two Nomad production clusters - one for the batch job, the other for services. 3r3188. All their business events they push in Kafka. From there we can pick them up. In fact, it is a lamba architecture. We can subscribe to HDFS with some services, do some real-time analytics, and at the same time we grab everything into ClickHouse and build dashboards to analyze all the business events for our services. We can analyze AV tests, whatever. 3r3197. And if we did not change the code, do not use feature toggles. We just tried to handle some kind of pipeline, he taught us a new model. We have a new path to it. We just change the Nomad-path to the model in the config, make the release of the new service, and here the Canary Deployment paradigm comes to the rescue, it is available in Nomad from the box. We have the current version of the service in three instances. We say that we want three canaries - three more replicas of new versions will be deployed without cutting down the old ones. As a result, traffic begins to split into two parts. Part of the traffic gets on new versions of services. All services push all their business events in Kafka. As a result, we can analyze metrics in real time. If everything is OK, then we can say that everything is OK. Deploy, Nomad will pass, gently turn off all the old versions and zaskelit new. This model is bad because if we need to bind version routing for some entity, User Item. This scheme does not work, because the traffic is balanced through round-robin. Therefore, we went the next way and sawed the service into two parts. This is the Gateway layer and the workers layer. The client communicates via HTTP with the Gateway layer, all the logic of selecting versions and balancing traffic is in the Gateway. At the same time, all the I /O Bound tasks that are needed to fulfill the prediction are also in the Gateway. Suppose we are in the prediction in the query comes userID, which we need to enrich some information. We have to pull other microservices and pick up all the information, features or databases. As a result, all this happens in Gateway. He communicates with workers who are only in the model, and does one thing - a prediction. Input and Output. But since we cut our service into two parts, an overhead appeared due to a remote network call. How to level this? It comes to the aid of the JRPS framework from Google, RPC from Google, which runs on top of HTTP2. You can use multiplexing and compression. JPRC is usedprotobuf pushes. This is a strongly typed binary protocol that has fast serialization and deserialization. As a result, we also have the ability to independently scale the Gateway and worker. Suppose we cannot keep any number of open HTTP connections. Okay, skeylim Gateway. We have a too slow prediction, we don’t have time to keep the load - okay, scale the workers. This approach fits very well on many-armed bandits. In Gateway, since all the logic of balancing traffic is implemented, it can go to external microservices and take all the statistics for each version from them, as well as make a decision on how to balance traffic. Let's say - with the help of Thompson Sampling. Everything was okay, the models were somehow trained, we prescribed them in the Nomad config. And what if there is a model of recommendations, which during the training already has time to become obsolete, and we need to constantly retrain them? Everything is done the same way: through periodic batch jobs some artifact is being produced - say, every three hours. At the same time, at the end of its work, the pipeline sets the path for a new model to Consul. This is the key value storage that is used for configuration. Nomad is able to template configs. Let there be an environment variable based on the values of key value storage Consul. He is following the changes and, as soon as a new path appears, he decides that there are two ways to go. He downloads the artifact itself through a new link, puts the service container into Docker with volume and reboots it - and does it all so that there is no downtime, that is, slowly, by the piece. Or it renders a new config and reports it to the service. Either the service itself will detect it - and inside it can independently, live, update its model card. That's all, thank you.