Serverless in the practice of programming education

Speaking of the word serverless, I think everyone should be familiar with it. So what does the word serverless mean? What problem can Serverless solve? Many of my friends may not have a deep experience and sense of body. In this article, I will talk about Serverless with you.

What is serverless

Let's take a look at the word Serverless first. Server, everyone knows the meaning of server, which means that the scope of the problem that Serverless solves is on the server side. Less, everyone must also know that it means less. Then serverless is connected, and then modified a little, that means less caring about the server.

Serverfull era

We all know that there will be two roles of R&D personnel and operation and maintenance personnel on the R&D side. When a new system is to be developed, R&D personnel start to write code development functions according to the PRD of the product manager. When the functions are developed and tested, they must be released. To the server. At this time, the operation and maintenance personnel began to plan the server specifications, the number of servers, the number of nodes deployed for each service, the expansion and contraction strategies and mechanisms of the servers, the process of publishing services, the elegant online and offline mechanism of services, and so on. This model is the separation of R&D and operation and maintenance, and the server operation and maintenance are handled by dedicated operation and maintenance personnel, and in many cases it is handled by pure human resources, which is the Serverfull era.

DevOps era

Who is the hardest thing in Internet companies? I believe most of them are operation and maintenance students. Do all kinds of network planning, environmental planning, database planning, etc. during the day, stay up late at night to release new versions to ensure the online guarantee, and many things are repetitive tasks. Then gradually there is the voice of empowering R&D. Operation and maintenance students help R&D students build a set of operation and maintenance consoles, allowing R&D students to publish services, view logs, and query data on the operation and maintenance console by themselves. In this way, the operation and maintenance students mainly maintain this operation and maintenance console system, and continue to improve the functions, which has made it a lot easier. This is the DevOps era of R&D and operation and maintenance.

Serverless era

Gradually, the focus of R&D students and operation and maintenance students is on the operation and maintenance console. The functions of the operation and maintenance console are getting more and more powerful. For example, according to the needs of the operation and maintenance side, the functions of automatic elastic expansion and performance monitoring have been added. According to the needs of the R&D side, the pipeline function of automated release has been added. Because of this system, code quality inspection, unit testing, package compilation, deployment, integration testing, gray release, flexible expansion, performance monitoring, and application protection are basically server-side tasks that require no human involvement. This is the era of NoOps, Serverless.

Application of Serverless in Programming Education

2020 is destined to be an extraordinary year. During the epidemic, how many companies fell like leeks, and how many companies sprung up like mushrooms, such as the online education industry.

That’s right, the online education industry is the biggest beneficiary of this epidemic. In this industry, there is a market segment for online programming education, especially for children and programming education for non-professionals, such as programming cats. , Zebra AI, Little Elephant Academy, etc. The online programming systems of these companies have some common characteristics and demands:

Write code on one side of the screen, execute the code, and display the running result on the other side.
The code written according to the question is a code block, and the amount of code for each question is not very large.
Run the code fast.
Support multiple programming languages.
It can support unpredictable traffic peaks.

For example, the programming class interface of Xiaoxiang Academy:

Serverless in the practice of programming education

Combining these characteristics and demands, it is not difficult to see that the core of constructing such an online programming system is to have a robust and highly available code operating environment that supports multiple programming languages.

So let's first take a look at the traditional implementation architecture:

Serverless in the practice of programming education

From the perspective of the High Level architecture, the front end only needs to pass the code snippets and programming language identification to the server, and then wait for the response to display the results. Therefore, the entire server side is responsible for classifying and preprocessing the codes of different languages, and then passing them to Runtime of different programming languages. This architecture has the following core issues.

Large workload and poor flexibility

The first is the problem of R&D and operation and maintenance workload. When there are new needs in the market or new business models need to be added, programming languages ​​need to be added. At this time, the R&D side needs to add programming code classification and preprocessing logic, and it needs to build corresponding programming. Runtime of the language. On the operation and maintenance side, it is necessary to plan the specifications and quantity of servers supporting the new language, as well as the overall CICD process. Therefore, the need to support new programming languages ​​needs to be implemented. It takes a lot of time for R&D and operation and maintenance. Coupled with the time for black/white box testing and CICD process testing, the support for market needs cannot respond quickly and flexibly. Relatively poor sex.

High availability

Secondly, the stability of the entire online programming system is the top priority. Therefore, all high-availability architectures for server-side services need to be built by themselves to ensure system stability in peak traffic scenarios and steady-state scenarios. On the one hand, high availability is whether the code logic is elegant and complete. On the other hand, it is the cluster for deploying services. Whether it is an ECS cluster or a K8s cluster, both R&D and operation and maintenance students need to plan together, so for the classification and pre-processing of programming languages In terms of services to be processed, a number of nodes can still be given, but for Runtime services in different languages, market demand will change at any time, so it is difficult to specifically measure the number of nodes for each service. Another important point is that all service expansion and shrinking mechanisms require operation and maintenance students to manually operate in real time. Even if automation is achieved through scripts, the speed of ECS bounce is far below business expectations.

Coarse cost control granularity

Third is the cost control of the entire IaaS resources. We all know that this kind of online education has obvious traffic tides, such as 10 am to 12 am, 3 pm to 5 pm, and 8 pm to 10 pm. When the traffic is relatively large, the traffic at other times is relatively small, and there is no traffic at night. Therefore, in this case, the traditional deployment architecture cannot match IaaS resources and traffic. For example, in order to cope with the peak traffic period, 20 ECS clusters are required to carry the traffic impact. At this time, the resource utilization rate of each ECS may be more than 70%, and the utilization rate is high, but when the traffic is small and at night , The resource utilization rate of each ECS may be more than ten percent or even lower, which is a waste of resources.

Serverless architecture

So let's take a look at how to use the Serverless architecture to achieve the same function and solve the above problems. When choosing Serverless products, the first thing that comes to mind in China is the products of Alibaba Cloud. Alibaba Cloud has two serverless architecture products, Serverless application engine and function computing. Here we use function computing to implement programming education scenarios. <br />Function Compute is an event-driven fully managed computing service, referred to as FC. Using functional computing, we don't need to purchase and manage infrastructure such as servers, just write and upload code. Function computing prepares computing resources for you, runs tasks flexibly and reliably, and provides functions such as log query, performance monitoring, and alarms.

I won't repeat the meaning of FC here, just give an example. There are two concepts in FC, one is service and the other is function. A service contains multiple functions:

Serverless in the practice of programming education

Here we take the Java microservice architecture to correspond. It can be understood that the service in FC is a class in Java, and the function in FC is a method in the Java class:

Serverless in the practice of programming education

But the method in the Java class can only be Java code, and the function in FC can set Runtime in different languages ​​to run different programming languages:

Serverless in the practice of programming education

After understanding this structure clearly, let's take a look at how to call the FC function, here will lead to the concept of a trigger. The HTTP request protocol we use most often is actually a type of trigger. It is called HTTP trigger in FC. In addition to HTTP triggers, OSS (Object Storage) triggers and SLS (Log Service) triggers are also provided. , Timing trigger, MNS trigger, CDN trigger, etc.

Serverless in the practice of programming education

As can be roughly understood from the above figure, we can call the function in FC in a variety of ways. For example, two scenarios. For example, whenever I upload a picture in a certain directory of the specified OSS Bucket, I can trigger a function in FC. The logic of the function is to download the picture just uploaded, and then process the picture , And then upload it back to OSS. For example, send a message to a queue of MNS, and then trigger the function in FC to process the logic for this message.

Finally, let's take a look at the high availability of FC. When each function runs the code, the bottom layer is definitely IaaS resources, but we only need to set the amount of memory required for each function to run the code, the minimum is 128M, the maximum is 3G, for users, there is no need to consider how many cores. , And you don’t need to know what server the code is running on, how many function instances are started, and you don’t need to care about elastic scaling, etc. These are all handled by FC.

Serverless in the practice of programming education

As can be seen from the above figure, there are two strategies for high availability:

Set the number of concurrent instances for the function. If it is set to 3, when three requests come in, the function will only start one instance, but it will start three threads to run the logic.
After the number of threads reaches the upper limit, another function instance will be pulled up.

As you see here, you may have a rough outline of the architecture of an online programming education system based on FC.

Serverless in the practice of programming education

The above picture is the architecture diagram of the online programming education system based on FC implementation. Under this architecture, let's see how to solve the above three core problems:

  • Workload and flexibility: We only need to focus on the business logic of how to execute the code. If we want to add a new language, we only need to create an FC function corresponding to the language Runtime.
  • High availability: Two layers of high availability guarantee for multi-threaded running business logic and multi-instance running business logic, and the expansion and contraction of function instances are all automatically processed by FC, without any configuration by R&D and operation and maintenance students.
  • Cost optimization: When there is no request, the function instance will not be pulled up, and there will be no billing at this time, so in the low traffic period or at night, the cost of the entire FC is very low. The number of function instances, billing granularity and traffic can be perfectly matched.

Serverless in the practice of programming education

Python programming language example

Let's take running Python code as an example to see how to use FC to implement Python online programming Demo. <br />####Create services and functions<br />Open the function computing (FC) console, select the corresponding Region, select the service/function on the left, and then create a new service:

Serverless in the practice of programming education

Output the service name and create the service.

Serverless in the practice of programming education

Enter the newly created service, then create a function, select HTTP function, you can configure the function of HTTP trigger:

Serverless in the practice of programming education

Set the parameters of the function:

Serverless in the practice of programming education

Several parameters that need attention are done here to illustrate:

  • Operating environment: This is easy to understand, choose Python3 here
  • Function instance types: There are two types of flexible instances and performance instances. The former supports up to 2C3G instances, and the latter supports larger specifications, up to 8C16G.
  • Function entry: see the document for details
  • HTTP trigger authentication method: anonymous means no authentication is required, while function requires authentication.

Code analysis

After the function is created, enter the function, you can see tabs such as overview, code execution, trigger, log query, etc., we first look at the trigger, we will see that this function automatically creates an HTTP trigger, and there is an HTTP corresponding to calling the function path:

Serverless in the practice of programming education

Then we select code execution and write our code directly online:

Serverless in the practice of programming education

The specific code is as follows:

-- coding: utf-8 --

import logging
import urllib.parse
import time
import subprocess
def handler(environ, start_response):
context = environ['fc.context']
request_uri = environ['fc.request_uri']
for k, v in environ.items():
if k.startswith('HTTP_'):
pass
try:
request_body_size = int(environ.get('CONTENT_LENGTH', 0))
except (ValueError):
request_body_size = 0
# 获取用户传入的code
request_body = environ['wsgi.input'].read(request_body_size)
codeStr = urllib.parse.unquote(request_body.decode("GBK"))
# 因为body里的对象里有code和input两个属性,这里分别获取用户code和用户输入
codeArr = codeStr.split('&')
code = codeArr[0][5:]
inputStr = codeArr[1][6:]
# 将用户code保存为py文件,放/tmp目录下,以时间戳为文件名
fileName = '/tmp/' + str(int(time.time())) + '.py'
f = open(fileName, "w")
# 这里预置引入了time库
f.write('import time \r\n')
f = open(fileName, "a")
f.write(code)
f.close()
# 创建子进程,执行刚才保存的用户code py文件
p = subprocess.Popen("python " + fileName, stdout=subprocess.PIPE, stdin=subprocess.PIPE, stderr=subprocess.PIPE, shell=True, encoding='utf-8')
# 通过标准输入传入用户的input输入
if inputStr != '' :
p.stdin.write(inputStr + "\n")
p.stdin.flush()
# 通过标准输出获取代码执行结果
r = p.stdout.read()
status = '200 OK'
response_headers = [('Content-type', 'text/plain')]
start_response(status, response_headers)
return [r.encode('UTF-8')]

The whole code idea is as follows:

  • The code snippet is passed in from the front end, the format is a string.
  • Get the incoming code string in the FC function, and intercept the code content and input content. Because the ability of input interaction in Python is simply realized here.
  • Save the code as a Python file, with the timestamp as the file name, and save it in the /tmp directory of the FC function. (Each FC function has a separate /tmp directory, which can store temporary files)
  • Then added the code to introduce the time library to the file to deal with interactive scenarios like sleep.
  • Create a subprocess through subprocess, and execute the Python file saved in the /tmp directory through the Python command in the way of Shell. If there is information input by the user, it is written to the child process through standard input and output.
  • Finally, the execution result is read back to the front end.

Front-end code

On the front end, I wrote a simple page using VUE. Here are two simple methods to analyze:

Serverless in the practice of programming education

The HTTP request object is initialized when the page is loaded, and the called HTTP path is the path of the HTTP trigger of the function just now.

Serverless in the practice of programming education

This method is to call the PythonRuntime function in FC and pass the code fragment of the front-end page to the function. The idea of ​​processing input interaction here is to scan the entire code segment, and divide the entire code segment into multiple segments using the input code as an identifier. Those that do not contain the input code are directly sent to the FC function for execution, and those that contain the input code, request input from the user, and then the code fragments are sent to the FC function for execution along with the information input by the user.

Serverless in the practice of programming education

The demonstration is as follows:

Serverless in the practice of programming education

Concluding remarks

This article introduces Serverless, Alibaba Cloud's Serverless product Function Computing (FC) and the Demo of an online programming system based on Function Computing (FC). Everyone should feel something. When implementing an online programming system based on function computing (FC), R&D students only need to focus on how to execute the code passed in from the front-end, and the entire server side does not require R&D students and operation and maintenance students to go Concern basically reflects the essence of Serverless.

There are many other application scenarios based on Serverless, and I will share them with you one by one.

Guess you like

Origin blog.51cto.com/14902238/2561338