Ballerina: Cloud-Native Middleware as a Programming Language by Anupama Pathirage


Video Transcription

Hello everyone. Thank you for uh the session. Joining for today's session. It's about Valera, a new programming language that we have developed at WSO two. So in this session, I'm going to cover some details about the language and I will do a quick demo as well.But before moving to the discussion, let me quickly ask a question from you. So if you see this image, there will be a lot of things coming to your mind if you are coming from the development background. So let's say you are coming from a software development background. There are so many things that you know about programming language. If someone is asking you what's the best programming language to learn or what is the best programming language to for a given scenario, then there will be thousands of questions in your mind. There are so many different programming languages and also so many different uh types of programming languages. So selecting one is not that easy. So that is where the problem started. So and then let's see what, what are the newest trends and the challenges as well to this software development or application development process, especially for developers. So this is taken from the IBM cloud native development report. So there it says um majority of the application it new application that is out of like 55% of the new application will be directly into the cloud and also existing applications like uh non cloud applications.

We will have to move them, majority of them to the cloud very soon. And even existing cloud applications, we will have to uh do some changes or customizations or add new features. So cloud development is not something that we can avoid. So it's the latest trend and also it has certain advan very, very high level of advantages, but it will introduce some uh challenges as well. So in this session, that's what I'm going to mainly cover uh why we need a new programming languages uh like language. And what is ballerina? Obviously some key features of ballerina, I will do a demo, a live recording demo to develop a rest service using ballerina. And finally, I will let you know how you guys can, can connect with ballerina to contribute to the Ballera open source development.

So if you look at the normal application development process, if you are coming from the software development background, you are, you should be very much familiar with this. So usually what we do is we gather the requirements and then uh we have to uh design the systems and then we have to implement, test it and build the system uh package it to the required artifacts and then finally deployed. And in the middle, we have some documentation steps as well. That's the usual process. But when it comes to the cloud, it's not ending there, there can be so many other steps, current deal with steps where we have to focus on so out of them. Uh So the application development process, usually we have to think about uh cloud aspects as well. Uh That means we have to build some of the do files and write deployment descriptors and also API management and security that plays a huge role there because we are developing something uh which are interacting with the network. So it's not that easy to manage security and also the access to it also the scaling and performance that also having a huge factor here because performance is very much important nowadays.

And also observing, like we need to analyze the usages and analytics and based on these factors, we have to redesign the application sometimes. So redesigning means we may have to adjust some of the things we have to re re implement the things by optimizing or maybe we will have to do some uh full code revamp as well. So that is a continuous process that we are talking about. So the next thing is uh why we need a new programming language, you know, there are many different programming languages in the world and uh all these programming languages are good at something uh specialize in some areas, some general purpose languages. So everything is there.

But the problem is now modern applications are good uh for general purpose programming language, general purpose applications. But sometimes the modern applications are very much uh integration heavy. That means we have to interact with multiple endpoints. And also that means interacting with network is something that we cannot avoid. So we need better network abstractions and also better support for network data or the data representation. Usually the data representation means we communicate with multiple different uh end points.

And that means we need to have a good support for uh data representation, transforming them and also maybe uh co convert them into different types or process them. So that's very much needed. And also the network aware type system uh to do all these things, we should have a very rich type system which is aware about the network as well. And then there are so many different network protocols. So we need to think about that as well. So the network protocols means uh GR PC web. So http webs sub there are so many protocols like that. So we need to have first class support for them as well. Another important thing is we need to think about the concurrency aspects uh integrating with multiple systems. We we can't forget about the concurrency network inductions are inherently concurrent. So we need to uh have better support for concurrency as well. And obviously, as I said before, cloud friendliness, which we can't avoid. That means developers should be able to bring their code into the cloud very easily. And then the visual representations usually when we develop a sophisticated application, it's very hard to I understand the code by just looking at the code. So if you can have some better visualization, that will be a great help. And finally, it should be a complete platform, not just a language that's also very important.

Because if it is just a language and if it doesn't have other support, like cloud support and other protocol support, then we will have to look for third party tools and other maybe other other supportive in ecosystem tools. So that is also kind of a problem for the developers. So that's why we thought we needed new programming language. So the motivation was we are from WSO, I'm from WSO two. We started this project in 2016 and it's a general purpose programming language, but it is specialized in solving integration and cloud based problems. And most important thing is although it's started and maintained by the WSO two, it's an open source project and driven by the open source community.

So anyone in the world can contribute and use it without any charge. And also this is a kind of a quick highlight of our timeline. We started in early 2016 as a synapse uh like as a replacement for existing synapse based applications at WSO two. And then it went through multiple iterations, development iterations. And finally, we released a first version of it at 2019. And in 2022 February, we released one leg patient. And also in the future, there are so many other plans uh to enhance the existing implementation as well as do the support for the release version. So that's high level timeline of ballerina. So then since I gave some background, let me quickly go through what is ballerina? So when it, when we say ballerina, what will go, what goes to your mind is some kind of a dance. Actually, it has some uh clear uh connection, connection with the dancing because we derived that name uh dance. Ballerina. Dance means it's kind of uh some synchronization. Dancers have to interact together and they have their own specific role to play and they have to work together as a team. So that's where the choreography come into the picture. And when we come, when it comes to networking instructions, it's very important the uh relevant communication is happening uh on time as that. So that's why the name and other than that nothing to do with the dancing. OK.

So the language is not object oriented, I would say because language has object concept and it has objects that means that if you want to uh associate some behaviors to the data objects are there. But we don't need to use it all the time. Uh Sometimes you might decide, OK, I I will just use functions instead of objects. And that has the flexibility to use objects of functions based on the requirement. And also our primary focus is on the data that is po data which is uh the network transmitted data. So we should be able to represent, describe and communicate data very easily. And another important aspect is that it's a compile language that means not like interpreted language. The at the compile time, we will generate things and also it's flexibly typed. That means usually statically typed languages are too rigid and dynamically typed languages may be too uh they are not that strict, but at the same time, there can be some type related issues. So Bagna stays in the middle. That means uh we do some checks at compile time and we do, we delay some of the checks into the run time. So we stay in the middle depending on the different situations. And also concurrency language has first class support for many different concurrency aspects. And it will help to write uh network intru program very easily. And other important thing is Bena is not a JB M language. It has two implementations. Actually the first one that we talked like the, the one that we released already is what we call J Ballerina.

It is written in Java to run on top of JVM. Uh But this is the first implementation to prove language, but it's production ready and anyone can use it for the production applications. Uh Then we need to remove this java binding. That's why we call N ballerina. It's compiled using LLBM. Uh N ballerina means native ballerina. It is uh it is generating the by codes directly into the platform architecture. So that project is on pro in progress and we will release in maybe one year, 1.5 years of time. So let me quickly go through some of the key features of ballerina. Uh So that as I mentioned before, it provides a better network abstractions. Usually network abstractions means if you are interacting with network, either we have to connect to some other endpoint as a client or we have to provide some service to the clients. So if you are talking to another endpoint, that means we need a client, client concept is coming from the language. And here you can see uh http client and uh http client is coming from the standard library http module, but client is a language concept and it has get paused to delete such http we verbs so that we can easily connect to another endpoint and services mean uh like this, you can see the syntax, it will listen on some port using a uh given listener and listener is based on the given protocol.

There can be different types of services based on the protocol. This is just an example of http service. And there are resource methods and remotes resource methods means you can basically uh it it gives a data oriented view to access and manipulate data and remote method means some uh network callable method. It will be useful for scenarios like graph Q. And as I mentioned before, we need network area type system, that is one of the key feature in ballerina. Uh We are dealing with play data, we need to exchange play data and there's a specific type in ballerina, what we call any data. And it can be directly serialized into JSN and many other formats in a very natural way. So handling data is very easy if you are dealing with ballerina code and also melena has structural type system, uh like records, table and maps arrays. So all these are there, we can convert between the different types very easily. So these are some of the common um types uh XML J and table. Some uh these are some uncommon types that we have in ballerina type system. And other important thing is code to cloud aspect. That means how we can bring our code into the cloud. So usually developers will have to write the develop uh various XML files, docker files to bring the code into the cloud.

But here mea is providing easy way we can specify the parameters to generate the relevant coon XML files or Docker images and it will generate all these things for on behalf of the developer. So it's just a matter of putting some uh giving this command and put some configurations if needed and then concurrency aspects, which we, it's actually huge topic. I won't be able to cover all this, all these things related to concurrency here today. But in high level, we have uh a synchronous function call that means we can start a function asynchronously and maybe we can wait for that output. And also we have the worker concept uh that represent a single strand of a function in location and strand concept strand is like a logical thread of control assigned to a given worker. Likewise, how many concepts to handle these concurrency aspects? And it's a huge topic, I'm not going to cover in detail today. Other important things like the reliability and the transactions. When it comes to transactions, we need to maintain the reliability and language itself provides index and semantics to transaction uh commit about and those kind of things.

And so handling those things will be very, is very easy if you are writing code in ballerina. And finally, one other important feature is Ballera provides sequence diagram. Uh If you can see this, it has code in left side and the diagram in the other side, uh we have ex exact 1 to 1 mapping between the code and the diagram. That means if you edit the code diagram will be autogenerated. And if you edit the diagram code will be generated. I will show my during my demo how it works. Uh So I will quickly move to the next one bean is a platform as I mentioned before. If it is just a language that won't be eno that, that is not enough for uh integration use cases or in a general purpose development test. Well, so we have the co language and also surrounding ecosystem as well. So this ecosystem is uh uh it, it consisted of many things, it has a standard library for various protocols and utilities and different module support and also module repository. So that anyone can uh publish the Accord and share that co, share that pale a code with others and also the documentation system to document the source code, the testing framework.

And also uh we code plugging support with all these features so that developers can easily use all these features with the tooling support. So that is about ballerina very quick over view in high level. Let me quickly show you some of the most popular uh modules in Ballina. This this is not not not all the modules but some part of the modules in the Ballera Center. We have different protocols support, http UDP, email uh or GRP, Croft, QL and so on. And also doctor coin support and any other cloud function, the support and also SAS application support and many utilities. Likewise, there are so many different uh protocols utilities and SARS vendor related uh modules so that developers can get their work done very easily.

So, in high level, that's about ballerina. Let me quickly go through the demo as well. I hope uh since this is a live demo, I hope it will run smoothly. Uh Let's see how it goes before moving to the demo. Let me quickly uh give you some idea about what I'm going to present today here. I'm going to search for books. Uh Let's say users are, users are willing to search for a given book uh by the title and then they want to return the uh ebook versions of it. Uh And also they need to see whether there's an associated movie with that title. So that what I'm going to use is I'm going to create a service called No Service and it will uh take accept the user request and in the background, it will go to the Google Books API to get the book details. And also it will query uh movie, the movie A P movies API to get the movie details. And also we had to do two request here. Uh because in the in this API first, it will return an ID E ID E for the given name. And then based on that ID, we have to fetch the data again to get the more details. So that's why there are two calls. So this is kind of a flow uh that I'm going to follow.

Uh I will do some coding and I will try to save time by copy pasting some of the codes as well. Uh Let's proceed with the demo. So I stopped sharing for now. Uh Let me share the full screen again so that you can see uh the entire so code and everything. Hope you all can see my screen. Uh Let me use my terminal here. Like here, I'm using Bell command. Bell is the bears bill system and package manager. I'm not going to use terminal for a long time. I'm just creating a new package. Bell has support for various other commands to various phases in the development process and also it has package related commands here. I'm going to use Bell new command uh to create a new package. So I'm using brand new no service. So it will create a new uh package called our service. So let me open it in VS code using the code command. I have already installed the VS code plug in uh and VS code uh application and ballerina plug in on it. So what it created is a main file. This is a regular main file that you see in any other language. It's just printing the hello world uh to the as the output. And I'm not interesting interested in uh hello world here. Actually. Uh I'm I'm going to define a service. So let me delete this entire code and let's start creating a service.

So to create a service, uh what I have to do is let uh let me get the support from codes in ballerina. I'm going to create a HTTP listener. So I use the code snippet and I'm going to fill it. The service is about books. So I'm going to name it, name the base part as books and it created HTTP listener which is listening on port zero. Let me quickly change it to 9090 service definition is good. It has imported http because this is based on http protocol to sh uh visualize the functionality. Let's say the resource function as well. So resource function means it is uh a get method. And also we have to provide the name, let's say search and we uh since users are providing the book title as the input parameter or query parameter, I edit it as well. So this is a simple resource. Let's return some value uh type from that as well. So usually uh A PS are returning Jason. So since bea is supporting Jason, I'm returning Jason here as well to test it. Let's return some dummy value. I'm returning this data here. So that's all, let's quickly run this program and see whether we can see the output. So I'm running with using Bell run command and I'm taking another terminal just to create the current request to test my service. Uh Let's say it's local host running on and on the port 1990 based part these books and then search with the name with the query parameter.

Let's search for the book called Maybe War and Peace for this place. I'm using these characters. OK? Everything is good. I'm sending the coil command and it has returned the test data. So everything seems good. Our first version of the service is working well. And now let's implement our scenario. That means we have to call the Google API first, let me show you how it looks like. Uh Here you can see this is the sample request search for the ONPS title and it returns a huge Jason. So we are going to return this Jason via our app our service in order to do that I need http client now. So let's create an HTTP client. Uh I'm going to name it as book A P endpoint and I'm going to create it using mute. And uh this part is this, I'm just copying it from here. I'm putting it here and all good. But you can see it as a red red line here. The problem is uh it is expect uh what I'm returning is http client. But the actual return type is client or error. This, this is because this is related to network instructions and it can always return errors. This is a feature in instead of throwing exceptions like other common languages, we are returning errors so that there are union types.

The pipe means union types and returning error means we have to check the errors whether the return type is an error or the actual type that we want. So to do that we can use if condition, if book endpoint is error, we can simply return an error. This is a dummy message or else we can process the real end point there. Uh Since union has two types, the first one is checked for the error. And with the type narrowing in ballerina, now it is reduced to http client. So we can directly use endpoint here and we can use G method. So what I'm going to get here is uh this is the query and I had to mention the title title is taken from the user input. So I'm concatenating the name, name is the input that I'm getting as in as the input to the resource. It's all good. And I had to return something since it's returning occasion, I'm returning books here. And since this is also a network interaction, it can return error any time due to network unavailability or anything. So everything is good, no errors. But then in order to process this JSN, I have to use another if first condition. So that is very difficult or inconvenient.

So we have introduced another thing called check if you go check clear here. That means we are returning the error at that point. So we don't need to have this union, we can simply avoid that. But since we are returning error, we have to pass it to the calling function. So we have to mark it as this function will return there and no error. Uh That means by returning a reso error from the resource function, it means the CTP listener will handle it and it will convert it into uh http 500 or 11 error. So we don't need this block anymore. We can simply remove it. And also here we can use check again. We eliminate this union error type. It's all good. And let me format the code a little bit. And instead of this test, Jason, now, since we have a Jason in hand, let's return that Jason and see whether we can ac access it. So let's rerun the program, it's running and it, it will take a bit of time to run. Yes, I everything is good. And I'm going to use uh JNP print tool to format the output. It's, you know, the API OK. We got the response. This is exactly the same response that we saw in the browser.

So it called the Google API and return it as the output of the Ballerina service. So this is the first part. Actually, we, we don't need to return it as it is, we need to process it a little bit. So what we have to do is then process this, Jason. Actually, we can process this Jason by just using the dot notation. Uh but it has some limitations because we don't know exactly what are the return fields and other things until the run time. If you are processing Jason and if someone wants to generate open api descriptions, it's very hard with like it's not easy with Jason because we don't know what is returning. So instead of that, what we can do is we can exactly define types for the given Jason to do that. That means we need to create a mapping record record type for this Jason. So by just looking at this, it will, you will feel like OK, this is a huge decision how we can create my pain structure. So Ballina is providing uh easy way to do that as well. So it has a tool uh to convert and given basically paste the Js NSA record. So I copy the JSN response from the browser and I pasted it. So it has created the all the required record types automatically. And it is a kind of big lengthy record list.

But I'm not going to uh basically, I'm not interested in all these details. I'm interested in only a few fields. Let's rename this into books for the simplicity and this to the items all good. And we have to rename this record as well. OK. So I'm not interested in these two fields so that I can remove this and even in the items, I'm only interested in it says some fields in the volume info and the self link that is the p uh link of the book and in the volume info, again, I'm interested in a few fields so that I can erase the other fields in the volume info.

Let's say I'm interested in title and uh average rating and others only these fields. So that means I can safely remove all the other records because although that it is returning the data, I'm not interested in that data. That is the concept of open record in Melaina. Uh Basically being a bit liberal about what we are receiving uh record type is open so that we specify the fields that we are interested that there can be more fields that we are not interested at. That's why the record is open. So we're good. And then what we have to do is instead of this Jason, we can simply return these books because we have created a mapping record. So now we can use that record instead of Jason. So now it's giving an error because uh the return type is a book which we uh mentioning as Jason here. So other important thing is like when we are returning something, we have to be exact about what we are returning, basically be conservative a little bit. When we are returning, we have to, we know exactly what we are returning. So we can define return types there, we have to create closed records. Sorry, we had to create closed records here. Uh You can notice this pipe notation that means it's a close record. So I'm going to exactly return the interested uh fields only. So what are the interested fields? Basically, I'm interested, it's the self link and these three fields. So that's, that's only that I'm going to return. So everything is good. Let's re rename this into book data.

And so what we are going to return is book data array. Uh So we have to create this book data array because the return, when we search for a title, it will return an array. We need to create an array here. So let me create a book data array as book data. Then I'm going to use another feature in ballerina that is query syntax. Uh For the moment, let's rename this re change this to a text uh so that it won't compile, give a compile time error. Uh query syntax means we can easily use uh usual sq like syntax to extract data. So there what I'm going to use is from items, I'm going to iterate this array, items, array and extract the data that I want for from items. Let's name is said item in books, items, I'm going to select the required fields only. So what we need is title, let's fill the exact values later, average rating and in for link. And so those are the things that I need. Uh I need the semicolon here. And now we can simply fill it using the real values item that well, you mean for title and item dot volume, mean for average rating item dot self? Ok. Uh Here infolink instead of infolink. Uh let me use authors. There's another field called authors. Let's use that. Let me quickly check from my original code. Yes, it's authors that I am interested at. I mistakenly deleted another field, uh authors and took another field. So this is also author. This is also all this all good. Sorry, I think I mistaken the delete this.

Yeah, wait, let's let me copy this. OK. All good uh self link and other information. And here I'm interested that item, not T blink means item dot clink and finally author's names, item dot volume in for all that. So everything seems good. Uh Let's quickly run it. So now we are going to return this book data instead of uh JSL that is the interested data set. So let's return book data from this resource function. So in front of this huge Jason, I should be able to return the required information, all the interested information only. Let's clean the screen and let's wait a bit to get it up and running. OK. It's running, it's, it was the same command as before. Uh Sorry, I think I have mistakenly. Yes. Uh Let me quickly copy because I changed somewhere in between. So if I copy from here Uh Let me quickly copy the full code so that uh uh it will be easy for me to demonstrate. I think I changed uh a little bit about the return types and that caused some error. And I'm not handling errors explicitly here to the simplicity. This is a full code so that I can show you the exact details and I'm running out of time a little bit. Uh So here, let me quickly run it while I'm explaining. So this is the same code. Uh Everything is in order. And also I called the other endpoint. That is the movie endpoint there. Also we need to create another http client because uh this is another different Endpoint and also it is written in different types.

So these different movie types also, I have defined them, I follow the same method. Uh We can basically, we can follow the same method that same utility to convert AJ N to the record here as well. And then it will create the required details and also the return types. Uh I had to define the exact return types as close records because that will be easy for the developers to uh generate open API descriptions. And also uh they know exactly what are the field types. So the it's running and let me quickly execute the command, it will take a bit of time because uh it is called three end points, three times three network calls. Now you can see the output. Actually, I skipped part of it because it's same, it's similar like we are dealing with network end points and creating records and processing the output. Uh Only important thing is like this query syntax I showed uh in my code, uh we, we are selecting the interested information and also we can put some filters here uh to filter out uh unnecessary values. Here. I am filtering out if the average rating is N I'm filtering it out.

So finally, after fetching from all the end points, this is the first movie endpoint call and this is the second endpoint call based on the idea, I'm extracting the ID here and calling that as well. So everything seems good and that's why we are returning uh combined record that is book data as well as the movie data. So book data is an array. So in the response you can see this is the book data array and this is the full uh full title and year and link for the movie. So actually I skipped part of it just for the simplicity. Uh And likewise, we can process any different types of data JSN XML and everything. So uh it it gives the flexibility to convert and transform maybe a process data in different uh formats. And that's a differential factor here. So then let me quickly go through the diagram here.

Uh So this is a generated diagram as I mentioned before. So diagram is autogenerated. Let's expand it a little bit. It has a good fun uh search function. I expanded it. Uh Let me close this. So you can see the diagram here. So the diagram is exactly similar to what we saw in the demo. Like what we have described basically it's calling book end point, getting some value and processing it. You can see the processing logic also and it has a movie end 0.2 calls to the movie end point. So everything is visible. This is not something, let's say we are adding some condition here. Uh Diaphragm is supported. Let's put some well, you like to uh so you can see the cord is generated as well. And then let's say let's simply do another dummy call here. You can see how the diagram is generated. So we added another call to the source code. Actually, we don't need it for the use case, but I just added it for visualization purpose. So the diagram is auto updated. So we have 1 to 1 mapping between the source code and diagram. That is the other important feature of DNA. So the final part I'm coming towards the end of my demo uh is about running or working in the cloud. So there, what you have to do is uh you have to build this using the cloud parameters. So usually what we have to do is we have to add cloud tel uh here with all the properties and other parameters.

But since I don't have any specific thing, I don't use it. And what I have to do is minus minus cloud, let's say coin is it will build quickly and it will generate the required articles in the target folder. We don't have anything at the moment and let's see how it goes. It will take a bit of time. It is generating, there are four steps and it's completing. OK? Everything seems good and it has generated the artifacts here. You can see the do file is auto generated with all the layers and it has taken the port from the cold and also in the Cooper artifacts. You can see the uh yeah, my files with all the descriptors. So that is something which is very easy for developers and uh so that they can generate the cloud artifacts easily. So that is actually end of my demo as well as my presentation about ballerina. Let me quickly go through the final step of it. Basically contributing. Ballerina Bena is an open source language and anyone in the world are are welcome to uh help Ballera or use ballerina by uh use or contribute it. So we have different communication channels. Uh Slack Google, the Google group, well, the Google group and also all the source code is free and open source in github platform. Uh So you can go and contribute. So that is about Valera.

I hope you get some understanding about the new language as well as some uh fun with the coding session. Thank you very much for joining.